root/drivers/media/i2c/smiapp/smiapp-core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. smiapp_read_frame_fmt
  2. smiapp_pll_configure
  3. smiapp_pll_try
  4. smiapp_pll_update
  5. __smiapp_update_exposure_limits
  6. smiapp_pixel_order
  7. smiapp_update_mbus_formats
  8. smiapp_set_ctrl
  9. smiapp_init_controls
  10. smiapp_init_late_controls
  11. smiapp_free_controls
  12. smiapp_get_limits
  13. smiapp_get_all_limits
  14. smiapp_get_limits_binning
  15. smiapp_get_mbus_formats
  16. smiapp_update_blanking
  17. smiapp_update_mode
  18. smiapp_read_nvm
  19. smiapp_change_cci_addr
  20. smiapp_setup_flash_strobe
  21. smiapp_power_on
  22. smiapp_power_off
  23. smiapp_start_streaming
  24. smiapp_stop_streaming
  25. smiapp_set_stream
  26. smiapp_enum_mbus_code
  27. __smiapp_get_mbus_code
  28. __smiapp_get_format
  29. smiapp_get_format
  30. smiapp_get_crop_compose
  31. smiapp_propagate
  32. smiapp_validate_csi_data_format
  33. smiapp_set_format_source
  34. smiapp_set_format
  35. scaling_goodness
  36. smiapp_set_compose_binner
  37. smiapp_set_compose_scaler
  38. smiapp_set_compose
  39. __smiapp_sel_supported
  40. smiapp_set_crop
  41. smiapp_get_native_size
  42. __smiapp_get_selection
  43. smiapp_get_selection
  44. smiapp_set_selection
  45. smiapp_get_skip_frames
  46. smiapp_get_skip_top_lines
  47. smiapp_sysfs_nvm_read
  48. smiapp_sysfs_ident_read
  49. smiapp_identify_module
  50. smiapp_register_subdev
  51. smiapp_unregistered
  52. smiapp_registered
  53. smiapp_cleanup
  54. smiapp_create_subdev
  55. smiapp_open
  56. smiapp_suspend
  57. smiapp_resume
  58. smiapp_get_hwconfig
  59. smiapp_probe
  60. smiapp_remove

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * drivers/media/i2c/smiapp/smiapp-core.c
   4  *
   5  * Generic driver for SMIA/SMIA++ compliant camera modules
   6  *
   7  * Copyright (C) 2010--2012 Nokia Corporation
   8  * Contact: Sakari Ailus <sakari.ailus@iki.fi>
   9  *
  10  * Based on smiapp driver by Vimarsh Zutshi
  11  * Based on jt8ev1.c by Vimarsh Zutshi
  12  * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
  13  */
  14 
  15 #include <linux/clk.h>
  16 #include <linux/delay.h>
  17 #include <linux/device.h>
  18 #include <linux/gpio.h>
  19 #include <linux/gpio/consumer.h>
  20 #include <linux/module.h>
  21 #include <linux/pm_runtime.h>
  22 #include <linux/property.h>
  23 #include <linux/regulator/consumer.h>
  24 #include <linux/slab.h>
  25 #include <linux/smiapp.h>
  26 #include <linux/v4l2-mediabus.h>
  27 #include <media/v4l2-fwnode.h>
  28 #include <media/v4l2-device.h>
  29 
  30 #include "smiapp.h"
  31 
  32 #define SMIAPP_ALIGN_DIM(dim, flags)    \
  33         ((flags) & V4L2_SEL_FLAG_GE     \
  34          ? ALIGN((dim), 2)              \
  35          : (dim) & ~1)
  36 
  37 /*
  38  * smiapp_module_idents - supported camera modules
  39  */
  40 static const struct smiapp_module_ident smiapp_module_idents[] = {
  41         SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
  42         SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
  43         SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
  44         SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
  45         SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
  46         SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
  47         SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
  48         SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
  49         SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
  50         SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
  51         SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
  52 };
  53 
  54 /*
  55  *
  56  * Dynamic Capability Identification
  57  *
  58  */
  59 
  60 static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
  61 {
  62         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  63         u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
  64         unsigned int i;
  65         int pixel_count = 0;
  66         int line_count = 0;
  67         int rval;
  68 
  69         rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
  70                            &fmt_model_type);
  71         if (rval)
  72                 return rval;
  73 
  74         rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
  75                            &fmt_model_subtype);
  76         if (rval)
  77                 return rval;
  78 
  79         ncol_desc = (fmt_model_subtype
  80                      & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
  81                 >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
  82         nrow_desc = fmt_model_subtype
  83                 & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;
  84 
  85         dev_dbg(&client->dev, "format_model_type %s\n",
  86                 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
  87                 ? "2 byte" :
  88                 fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
  89                 ? "4 byte" : "is simply bad");
  90 
  91         for (i = 0; i < ncol_desc + nrow_desc; i++) {
  92                 u32 desc;
  93                 u32 pixelcode;
  94                 u32 pixels;
  95                 char *which;
  96                 char *what;
  97                 u32 reg;
  98 
  99                 if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
 100                         reg = SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i);
 101                         rval = smiapp_read(sensor, reg, &desc);
 102                         if (rval)
 103                                 return rval;
 104 
 105                         pixelcode =
 106                                 (desc
 107                                  & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
 108                                 >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
 109                         pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
 110                 } else if (fmt_model_type
 111                            == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
 112                         reg = SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i);
 113                         rval = smiapp_read(sensor, reg, &desc);
 114                         if (rval)
 115                                 return rval;
 116 
 117                         pixelcode =
 118                                 (desc
 119                                  & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
 120                                 >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
 121                         pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
 122                 } else {
 123                         dev_dbg(&client->dev,
 124                                 "invalid frame format model type %d\n",
 125                                 fmt_model_type);
 126                         return -EINVAL;
 127                 }
 128 
 129                 if (i < ncol_desc)
 130                         which = "columns";
 131                 else
 132                         which = "rows";
 133 
 134                 switch (pixelcode) {
 135                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
 136                         what = "embedded";
 137                         break;
 138                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
 139                         what = "dummy";
 140                         break;
 141                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
 142                         what = "black";
 143                         break;
 144                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
 145                         what = "dark";
 146                         break;
 147                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
 148                         what = "visible";
 149                         break;
 150                 default:
 151                         what = "invalid";
 152                         break;
 153                 }
 154 
 155                 dev_dbg(&client->dev,
 156                         "0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg,
 157                         what, pixels, which, pixelcode);
 158 
 159                 if (i < ncol_desc) {
 160                         if (pixelcode ==
 161                             SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE)
 162                                 sensor->visible_pixel_start = pixel_count;
 163                         pixel_count += pixels;
 164                         continue;
 165                 }
 166 
 167                 /* Handle row descriptors */
 168                 switch (pixelcode) {
 169                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
 170                         if (sensor->embedded_end)
 171                                 break;
 172                         sensor->embedded_start = line_count;
 173                         sensor->embedded_end = line_count + pixels;
 174                         break;
 175                 case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
 176                         sensor->image_start = line_count;
 177                         break;
 178                 }
 179                 line_count += pixels;
 180         }
 181 
 182         if (sensor->embedded_end > sensor->image_start) {
 183                 dev_dbg(&client->dev,
 184                         "adjusting image start line to %u (was %u)\n",
 185                         sensor->embedded_end, sensor->image_start);
 186                 sensor->image_start = sensor->embedded_end;
 187         }
 188 
 189         dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
 190                 sensor->embedded_start, sensor->embedded_end);
 191         dev_dbg(&client->dev, "image data starts at line %d\n",
 192                 sensor->image_start);
 193 
 194         return 0;
 195 }
 196 
 197 static int smiapp_pll_configure(struct smiapp_sensor *sensor)
 198 {
 199         struct smiapp_pll *pll = &sensor->pll;
 200         int rval;
 201 
 202         rval = smiapp_write(
 203                 sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt.pix_clk_div);
 204         if (rval < 0)
 205                 return rval;
 206 
 207         rval = smiapp_write(
 208                 sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt.sys_clk_div);
 209         if (rval < 0)
 210                 return rval;
 211 
 212         rval = smiapp_write(
 213                 sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
 214         if (rval < 0)
 215                 return rval;
 216 
 217         rval = smiapp_write(
 218                 sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
 219         if (rval < 0)
 220                 return rval;
 221 
 222         /* Lane op clock ratio does not apply here. */
 223         rval = smiapp_write(
 224                 sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
 225                 DIV_ROUND_UP(pll->op.sys_clk_freq_hz, 1000000 / 256 / 256));
 226         if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
 227                 return rval;
 228 
 229         rval = smiapp_write(
 230                 sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op.pix_clk_div);
 231         if (rval < 0)
 232                 return rval;
 233 
 234         return smiapp_write(
 235                 sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op.sys_clk_div);
 236 }
 237 
 238 static int smiapp_pll_try(struct smiapp_sensor *sensor,
 239                           struct smiapp_pll *pll)
 240 {
 241         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 242         struct smiapp_pll_limits lim = {
 243                 .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
 244                 .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
 245                 .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
 246                 .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
 247                 .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
 248                 .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
 249                 .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
 250                 .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],
 251 
 252                 .op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
 253                 .op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
 254                 .op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
 255                 .op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
 256                 .op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
 257                 .op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
 258                 .op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
 259                 .op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
 260 
 261                 .vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
 262                 .vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
 263                 .vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
 264                 .vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
 265                 .vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
 266                 .vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
 267                 .vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
 268                 .vt.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
 269 
 270                 .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
 271                 .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
 272         };
 273 
 274         return smiapp_pll_calculate(&client->dev, &lim, pll);
 275 }
 276 
 277 static int smiapp_pll_update(struct smiapp_sensor *sensor)
 278 {
 279         struct smiapp_pll *pll = &sensor->pll;
 280         int rval;
 281 
 282         pll->binning_horizontal = sensor->binning_horizontal;
 283         pll->binning_vertical = sensor->binning_vertical;
 284         pll->link_freq =
 285                 sensor->link_freq->qmenu_int[sensor->link_freq->val];
 286         pll->scale_m = sensor->scale_m;
 287         pll->bits_per_pixel = sensor->csi_format->compressed;
 288 
 289         rval = smiapp_pll_try(sensor, pll);
 290         if (rval < 0)
 291                 return rval;
 292 
 293         __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
 294                                  pll->pixel_rate_pixel_array);
 295         __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
 296 
 297         return 0;
 298 }
 299 
 300 
 301 /*
 302  *
 303  * V4L2 Controls handling
 304  *
 305  */
 306 
 307 static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
 308 {
 309         struct v4l2_ctrl *ctrl = sensor->exposure;
 310         int max;
 311 
 312         max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
 313                 + sensor->vblank->val
 314                 - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
 315 
 316         __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
 317 }
 318 
 319 /*
 320  * Order matters.
 321  *
 322  * 1. Bits-per-pixel, descending.
 323  * 2. Bits-per-pixel compressed, descending.
 324  * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
 325  *    orders must be defined.
 326  */
 327 static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
 328         { MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GRBG, },
 329         { MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_RGGB, },
 330         { MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_BGGR, },
 331         { MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GBRG, },
 332         { MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GRBG, },
 333         { MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_RGGB, },
 334         { MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_BGGR, },
 335         { MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GBRG, },
 336         { MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
 337         { MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
 338         { MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
 339         { MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
 340         { MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
 341         { MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
 342         { MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
 343         { MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
 344         { MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
 345         { MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
 346         { MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
 347         { MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
 348         { MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
 349         { MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
 350         { MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
 351         { MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
 352 };
 353 
 354 static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
 355 
 356 #define to_csi_format_idx(fmt) (((unsigned long)(fmt)                   \
 357                                  - (unsigned long)smiapp_csi_data_formats) \
 358                                 / sizeof(*smiapp_csi_data_formats))
 359 
 360 static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
 361 {
 362         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 363         int flip = 0;
 364 
 365         if (sensor->hflip) {
 366                 if (sensor->hflip->val)
 367                         flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
 368 
 369                 if (sensor->vflip->val)
 370                         flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
 371         }
 372 
 373         flip ^= sensor->hvflip_inv_mask;
 374 
 375         dev_dbg(&client->dev, "flip %d\n", flip);
 376         return sensor->default_pixel_order ^ flip;
 377 }
 378 
 379 static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
 380 {
 381         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 382         unsigned int csi_format_idx =
 383                 to_csi_format_idx(sensor->csi_format) & ~3;
 384         unsigned int internal_csi_format_idx =
 385                 to_csi_format_idx(sensor->internal_csi_format) & ~3;
 386         unsigned int pixel_order = smiapp_pixel_order(sensor);
 387 
 388         sensor->mbus_frame_fmts =
 389                 sensor->default_mbus_frame_fmts << pixel_order;
 390         sensor->csi_format =
 391                 &smiapp_csi_data_formats[csi_format_idx + pixel_order];
 392         sensor->internal_csi_format =
 393                 &smiapp_csi_data_formats[internal_csi_format_idx
 394                                          + pixel_order];
 395 
 396         BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
 397                >= ARRAY_SIZE(smiapp_csi_data_formats));
 398 
 399         dev_dbg(&client->dev, "new pixel order %s\n",
 400                 pixel_order_str[pixel_order]);
 401 }
 402 
 403 static const char * const smiapp_test_patterns[] = {
 404         "Disabled",
 405         "Solid Colour",
 406         "Eight Vertical Colour Bars",
 407         "Colour Bars With Fade to Grey",
 408         "Pseudorandom Sequence (PN9)",
 409 };
 410 
 411 static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
 412 {
 413         struct smiapp_sensor *sensor =
 414                 container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
 415                         ->sensor;
 416         u32 orient = 0;
 417         int exposure;
 418         int rval;
 419 
 420         switch (ctrl->id) {
 421         case V4L2_CID_ANALOGUE_GAIN:
 422                 return smiapp_write(
 423                         sensor,
 424                         SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
 425 
 426         case V4L2_CID_EXPOSURE:
 427                 return smiapp_write(
 428                         sensor,
 429                         SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
 430 
 431         case V4L2_CID_HFLIP:
 432         case V4L2_CID_VFLIP:
 433                 if (sensor->streaming)
 434                         return -EBUSY;
 435 
 436                 if (sensor->hflip->val)
 437                         orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
 438 
 439                 if (sensor->vflip->val)
 440                         orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
 441 
 442                 orient ^= sensor->hvflip_inv_mask;
 443                 rval = smiapp_write(sensor, SMIAPP_REG_U8_IMAGE_ORIENTATION,
 444                                     orient);
 445                 if (rval < 0)
 446                         return rval;
 447 
 448                 smiapp_update_mbus_formats(sensor);
 449 
 450                 return 0;
 451 
 452         case V4L2_CID_VBLANK:
 453                 exposure = sensor->exposure->val;
 454 
 455                 __smiapp_update_exposure_limits(sensor);
 456 
 457                 if (exposure > sensor->exposure->maximum) {
 458                         sensor->exposure->val = sensor->exposure->maximum;
 459                         rval = smiapp_set_ctrl(sensor->exposure);
 460                         if (rval < 0)
 461                                 return rval;
 462                 }
 463 
 464                 return smiapp_write(
 465                         sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
 466                         sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
 467                         + ctrl->val);
 468 
 469         case V4L2_CID_HBLANK:
 470                 return smiapp_write(
 471                         sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
 472                         sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
 473                         + ctrl->val);
 474 
 475         case V4L2_CID_LINK_FREQ:
 476                 if (sensor->streaming)
 477                         return -EBUSY;
 478 
 479                 return smiapp_pll_update(sensor);
 480 
 481         case V4L2_CID_TEST_PATTERN: {
 482                 unsigned int i;
 483 
 484                 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
 485                         v4l2_ctrl_activate(
 486                                 sensor->test_data[i],
 487                                 ctrl->val ==
 488                                 V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
 489 
 490                 return smiapp_write(
 491                         sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
 492         }
 493 
 494         case V4L2_CID_TEST_PATTERN_RED:
 495                 return smiapp_write(
 496                         sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);
 497 
 498         case V4L2_CID_TEST_PATTERN_GREENR:
 499                 return smiapp_write(
 500                         sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);
 501 
 502         case V4L2_CID_TEST_PATTERN_BLUE:
 503                 return smiapp_write(
 504                         sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);
 505 
 506         case V4L2_CID_TEST_PATTERN_GREENB:
 507                 return smiapp_write(
 508                         sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);
 509 
 510         case V4L2_CID_PIXEL_RATE:
 511                 /* For v4l2_ctrl_s_ctrl_int64() used internally. */
 512                 return 0;
 513 
 514         default:
 515                 return -EINVAL;
 516         }
 517 }
 518 
 519 static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
 520         .s_ctrl = smiapp_set_ctrl,
 521 };
 522 
 523 static int smiapp_init_controls(struct smiapp_sensor *sensor)
 524 {
 525         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 526         int rval;
 527 
 528         rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
 529         if (rval)
 530                 return rval;
 531 
 532         sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
 533 
 534         sensor->analog_gain = v4l2_ctrl_new_std(
 535                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 536                 V4L2_CID_ANALOGUE_GAIN,
 537                 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
 538                 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
 539                 max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
 540                 sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
 541 
 542         /* Exposure limits will be updated soon, use just something here. */
 543         sensor->exposure = v4l2_ctrl_new_std(
 544                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 545                 V4L2_CID_EXPOSURE, 0, 0, 1, 0);
 546 
 547         sensor->hflip = v4l2_ctrl_new_std(
 548                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 549                 V4L2_CID_HFLIP, 0, 1, 1, 0);
 550         sensor->vflip = v4l2_ctrl_new_std(
 551                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 552                 V4L2_CID_VFLIP, 0, 1, 1, 0);
 553 
 554         sensor->vblank = v4l2_ctrl_new_std(
 555                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 556                 V4L2_CID_VBLANK, 0, 1, 1, 0);
 557 
 558         if (sensor->vblank)
 559                 sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
 560 
 561         sensor->hblank = v4l2_ctrl_new_std(
 562                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 563                 V4L2_CID_HBLANK, 0, 1, 1, 0);
 564 
 565         if (sensor->hblank)
 566                 sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
 567 
 568         sensor->pixel_rate_parray = v4l2_ctrl_new_std(
 569                 &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
 570                 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
 571 
 572         v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
 573                                      &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
 574                                      ARRAY_SIZE(smiapp_test_patterns) - 1,
 575                                      0, 0, smiapp_test_patterns);
 576 
 577         if (sensor->pixel_array->ctrl_handler.error) {
 578                 dev_err(&client->dev,
 579                         "pixel array controls initialization failed (%d)\n",
 580                         sensor->pixel_array->ctrl_handler.error);
 581                 return sensor->pixel_array->ctrl_handler.error;
 582         }
 583 
 584         sensor->pixel_array->sd.ctrl_handler =
 585                 &sensor->pixel_array->ctrl_handler;
 586 
 587         v4l2_ctrl_cluster(2, &sensor->hflip);
 588 
 589         rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
 590         if (rval)
 591                 return rval;
 592 
 593         sensor->src->ctrl_handler.lock = &sensor->mutex;
 594 
 595         sensor->pixel_rate_csi = v4l2_ctrl_new_std(
 596                 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
 597                 V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
 598 
 599         if (sensor->src->ctrl_handler.error) {
 600                 dev_err(&client->dev,
 601                         "src controls initialization failed (%d)\n",
 602                         sensor->src->ctrl_handler.error);
 603                 return sensor->src->ctrl_handler.error;
 604         }
 605 
 606         sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
 607 
 608         return 0;
 609 }
 610 
 611 /*
 612  * For controls that require information on available media bus codes
 613  * and linke frequencies.
 614  */
 615 static int smiapp_init_late_controls(struct smiapp_sensor *sensor)
 616 {
 617         unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
 618                 sensor->csi_format->compressed - sensor->compressed_min_bpp];
 619         unsigned int i;
 620 
 621         for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
 622                 int max_value = (1 << sensor->csi_format->width) - 1;
 623 
 624                 sensor->test_data[i] = v4l2_ctrl_new_std(
 625                                 &sensor->pixel_array->ctrl_handler,
 626                                 &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
 627                                 0, max_value, 1, max_value);
 628         }
 629 
 630         sensor->link_freq = v4l2_ctrl_new_int_menu(
 631                 &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
 632                 V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
 633                 __ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
 634 
 635         return sensor->src->ctrl_handler.error;
 636 }
 637 
 638 static void smiapp_free_controls(struct smiapp_sensor *sensor)
 639 {
 640         unsigned int i;
 641 
 642         for (i = 0; i < sensor->ssds_used; i++)
 643                 v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
 644 }
 645 
 646 static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
 647                              unsigned int n)
 648 {
 649         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 650         unsigned int i;
 651         u32 val;
 652         int rval;
 653 
 654         for (i = 0; i < n; i++) {
 655                 rval = smiapp_read(
 656                         sensor, smiapp_reg_limits[limit[i]].addr, &val);
 657                 if (rval)
 658                         return rval;
 659                 sensor->limits[limit[i]] = val;
 660                 dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
 661                         smiapp_reg_limits[limit[i]].addr,
 662                         smiapp_reg_limits[limit[i]].what, val, val);
 663         }
 664 
 665         return 0;
 666 }
 667 
 668 static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
 669 {
 670         unsigned int i;
 671         int rval;
 672 
 673         for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
 674                 rval = smiapp_get_limits(sensor, &i, 1);
 675                 if (rval < 0)
 676                         return rval;
 677         }
 678 
 679         if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
 680                 smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
 681 
 682         return 0;
 683 }
 684 
 685 static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
 686 {
 687         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 688         static u32 const limits[] = {
 689                 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
 690                 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
 691                 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
 692                 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
 693                 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
 694                 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
 695                 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
 696         };
 697         static u32 const limits_replace[] = {
 698                 SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
 699                 SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
 700                 SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
 701                 SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
 702                 SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
 703                 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
 704                 SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
 705         };
 706         unsigned int i;
 707         int rval;
 708 
 709         if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
 710             SMIAPP_BINNING_CAPABILITY_NO) {
 711                 for (i = 0; i < ARRAY_SIZE(limits); i++)
 712                         sensor->limits[limits[i]] =
 713                                 sensor->limits[limits_replace[i]];
 714 
 715                 return 0;
 716         }
 717 
 718         rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
 719         if (rval < 0)
 720                 return rval;
 721 
 722         /*
 723          * Sanity check whether the binning limits are valid. If not,
 724          * use the non-binning ones.
 725          */
 726         if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
 727             && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
 728             && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
 729                 return 0;
 730 
 731         for (i = 0; i < ARRAY_SIZE(limits); i++) {
 732                 dev_dbg(&client->dev,
 733                         "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
 734                         smiapp_reg_limits[limits[i]].addr,
 735                         smiapp_reg_limits[limits[i]].what,
 736                         sensor->limits[limits_replace[i]],
 737                         sensor->limits[limits_replace[i]]);
 738                 sensor->limits[limits[i]] =
 739                         sensor->limits[limits_replace[i]];
 740         }
 741 
 742         return 0;
 743 }
 744 
 745 static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
 746 {
 747         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 748         struct smiapp_pll *pll = &sensor->pll;
 749         u8 compressed_max_bpp = 0;
 750         unsigned int type, n;
 751         unsigned int i, pixel_order;
 752         int rval;
 753 
 754         rval = smiapp_read(
 755                 sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
 756         if (rval)
 757                 return rval;
 758 
 759         dev_dbg(&client->dev, "data_format_model_type %d\n", type);
 760 
 761         rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
 762                            &pixel_order);
 763         if (rval)
 764                 return rval;
 765 
 766         if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
 767                 dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
 768                 return -EINVAL;
 769         }
 770 
 771         dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
 772                 pixel_order_str[pixel_order]);
 773 
 774         switch (type) {
 775         case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
 776                 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
 777                 break;
 778         case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
 779                 n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
 780                 break;
 781         default:
 782                 return -EINVAL;
 783         }
 784 
 785         sensor->default_pixel_order = pixel_order;
 786         sensor->mbus_frame_fmts = 0;
 787 
 788         for (i = 0; i < n; i++) {
 789                 unsigned int fmt, j;
 790 
 791                 rval = smiapp_read(
 792                         sensor,
 793                         SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
 794                 if (rval)
 795                         return rval;
 796 
 797                 dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
 798                         i, fmt >> 8, (u8)fmt);
 799 
 800                 for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
 801                         const struct smiapp_csi_data_format *f =
 802                                 &smiapp_csi_data_formats[j];
 803 
 804                         if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
 805                                 continue;
 806 
 807                         if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
 808                                 continue;
 809 
 810                         dev_dbg(&client->dev, "jolly good! %d\n", j);
 811 
 812                         sensor->default_mbus_frame_fmts |= 1 << j;
 813                 }
 814         }
 815 
 816         /* Figure out which BPP values can be used with which formats. */
 817         pll->binning_horizontal = 1;
 818         pll->binning_vertical = 1;
 819         pll->scale_m = sensor->scale_m;
 820 
 821         for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
 822                 sensor->compressed_min_bpp =
 823                         min(smiapp_csi_data_formats[i].compressed,
 824                             sensor->compressed_min_bpp);
 825                 compressed_max_bpp =
 826                         max(smiapp_csi_data_formats[i].compressed,
 827                             compressed_max_bpp);
 828         }
 829 
 830         sensor->valid_link_freqs = devm_kcalloc(
 831                 &client->dev,
 832                 compressed_max_bpp - sensor->compressed_min_bpp + 1,
 833                 sizeof(*sensor->valid_link_freqs), GFP_KERNEL);
 834         if (!sensor->valid_link_freqs)
 835                 return -ENOMEM;
 836 
 837         for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
 838                 const struct smiapp_csi_data_format *f =
 839                         &smiapp_csi_data_formats[i];
 840                 unsigned long *valid_link_freqs =
 841                         &sensor->valid_link_freqs[
 842                                 f->compressed - sensor->compressed_min_bpp];
 843                 unsigned int j;
 844 
 845                 if (!(sensor->default_mbus_frame_fmts & 1 << i))
 846                         continue;
 847 
 848                 pll->bits_per_pixel = f->compressed;
 849 
 850                 for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
 851                         pll->link_freq = sensor->hwcfg->op_sys_clock[j];
 852 
 853                         rval = smiapp_pll_try(sensor, pll);
 854                         dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
 855                                 pll->link_freq, pll->bits_per_pixel,
 856                                 rval ? "not ok" : "ok");
 857                         if (rval)
 858                                 continue;
 859 
 860                         set_bit(j, valid_link_freqs);
 861                 }
 862 
 863                 if (!*valid_link_freqs) {
 864                         dev_info(&client->dev,
 865                                  "no valid link frequencies for %u bpp\n",
 866                                  f->compressed);
 867                         sensor->default_mbus_frame_fmts &= ~BIT(i);
 868                         continue;
 869                 }
 870 
 871                 if (!sensor->csi_format
 872                     || f->width > sensor->csi_format->width
 873                     || (f->width == sensor->csi_format->width
 874                         && f->compressed > sensor->csi_format->compressed)) {
 875                         sensor->csi_format = f;
 876                         sensor->internal_csi_format = f;
 877                 }
 878         }
 879 
 880         if (!sensor->csi_format) {
 881                 dev_err(&client->dev, "no supported mbus code found\n");
 882                 return -EINVAL;
 883         }
 884 
 885         smiapp_update_mbus_formats(sensor);
 886 
 887         return 0;
 888 }
 889 
 890 static void smiapp_update_blanking(struct smiapp_sensor *sensor)
 891 {
 892         struct v4l2_ctrl *vblank = sensor->vblank;
 893         struct v4l2_ctrl *hblank = sensor->hblank;
 894         int min, max;
 895 
 896         min = max_t(int,
 897                     sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
 898                     sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
 899                     sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
 900         max = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
 901                 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
 902 
 903         __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
 904 
 905         min = max_t(int,
 906                     sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
 907                     sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
 908                     sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
 909         max = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
 910                 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
 911 
 912         __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
 913 
 914         __smiapp_update_exposure_limits(sensor);
 915 }
 916 
 917 static int smiapp_update_mode(struct smiapp_sensor *sensor)
 918 {
 919         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
 920         unsigned int binning_mode;
 921         int rval;
 922 
 923         /* Binning has to be set up here; it affects limits */
 924         if (sensor->binning_horizontal == 1 &&
 925             sensor->binning_vertical == 1) {
 926                 binning_mode = 0;
 927         } else {
 928                 u8 binning_type =
 929                         (sensor->binning_horizontal << 4)
 930                         | sensor->binning_vertical;
 931 
 932                 rval = smiapp_write(
 933                         sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
 934                 if (rval < 0)
 935                         return rval;
 936 
 937                 binning_mode = 1;
 938         }
 939         rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
 940         if (rval < 0)
 941                 return rval;
 942 
 943         /* Get updated limits due to binning */
 944         rval = smiapp_get_limits_binning(sensor);
 945         if (rval < 0)
 946                 return rval;
 947 
 948         rval = smiapp_pll_update(sensor);
 949         if (rval < 0)
 950                 return rval;
 951 
 952         /* Output from pixel array, including blanking */
 953         smiapp_update_blanking(sensor);
 954 
 955         dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
 956         dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
 957 
 958         dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
 959                 sensor->pll.pixel_rate_pixel_array /
 960                 ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
 961                   + sensor->hblank->val) *
 962                  (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
 963                   + sensor->vblank->val) / 100));
 964 
 965         return 0;
 966 }
 967 
 968 /*
 969  *
 970  * SMIA++ NVM handling
 971  *
 972  */
 973 static int smiapp_read_nvm(struct smiapp_sensor *sensor,
 974                            unsigned char *nvm)
 975 {
 976         u32 i, s, p, np, v;
 977         int rval = 0, rval2;
 978 
 979         np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
 980         for (p = 0; p < np; p++) {
 981                 rval = smiapp_write(
 982                         sensor,
 983                         SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
 984                 if (rval)
 985                         goto out;
 986 
 987                 rval = smiapp_write(sensor,
 988                                     SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
 989                                     SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
 990                                     SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
 991                 if (rval)
 992                         goto out;
 993 
 994                 for (i = 1000; i > 0; i--) {
 995                         rval = smiapp_read(
 996                                 sensor,
 997                                 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
 998 
 999                         if (rval)
1000                                 goto out;
1001 
1002                         if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
1003                                 break;
1004 
1005                 }
1006                 if (!i) {
1007                         rval = -ETIMEDOUT;
1008                         goto out;
1009                 }
1010 
1011                 for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
1012                         rval = smiapp_read(
1013                                 sensor,
1014                                 SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
1015                                 &v);
1016                         if (rval)
1017                                 goto out;
1018 
1019                         *nvm++ = v;
1020                 }
1021         }
1022 
1023 out:
1024         rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
1025         if (rval < 0)
1026                 return rval;
1027         else
1028                 return rval2;
1029 }
1030 
1031 /*
1032  *
1033  * SMIA++ CCI address control
1034  *
1035  */
1036 static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
1037 {
1038         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1039         int rval;
1040         u32 val;
1041 
1042         client->addr = sensor->hwcfg->i2c_addr_dfl;
1043 
1044         rval = smiapp_write(sensor,
1045                             SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1046                             sensor->hwcfg->i2c_addr_alt << 1);
1047         if (rval)
1048                 return rval;
1049 
1050         client->addr = sensor->hwcfg->i2c_addr_alt;
1051 
1052         /* verify addr change went ok */
1053         rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
1054         if (rval)
1055                 return rval;
1056 
1057         if (val != sensor->hwcfg->i2c_addr_alt << 1)
1058                 return -ENODEV;
1059 
1060         return 0;
1061 }
1062 
1063 /*
1064  *
1065  * SMIA++ Mode Control
1066  *
1067  */
1068 static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
1069 {
1070         struct smiapp_flash_strobe_parms *strobe_setup;
1071         unsigned int ext_freq = sensor->hwcfg->ext_clk;
1072         u32 tmp;
1073         u32 strobe_adjustment;
1074         u32 strobe_width_high_rs;
1075         int rval;
1076 
1077         strobe_setup = sensor->hwcfg->strobe_setup;
1078 
1079         /*
1080          * How to calculate registers related to strobe length. Please
1081          * do not change, or if you do at least know what you're
1082          * doing. :-)
1083          *
1084          * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
1085          *
1086          * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
1087          *      / EXTCLK freq [Hz]) * flash_strobe_adjustment
1088          *
1089          * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
1090          * flash_strobe_adjustment E N, [1 - 0xff]
1091          *
1092          * The formula above is written as below to keep it on one
1093          * line:
1094          *
1095          * l / 10^6 = w / e * a
1096          *
1097          * Let's mark w * a by x:
1098          *
1099          * x = w * a
1100          *
1101          * Thus, we get:
1102          *
1103          * x = l * e / 10^6
1104          *
1105          * The strobe width must be at least as long as requested,
1106          * thus rounding upwards is needed.
1107          *
1108          * x = (l * e + 10^6 - 1) / 10^6
1109          * -----------------------------
1110          *
1111          * Maximum possible accuracy is wanted at all times. Thus keep
1112          * a as small as possible.
1113          *
1114          * Calculate a, assuming maximum w, with rounding upwards:
1115          *
1116          * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
1117          * -------------------------------------
1118          *
1119          * Thus, we also get w, with that a, with rounding upwards:
1120          *
1121          * w = (x + a - 1) / a
1122          * -------------------
1123          *
1124          * To get limits:
1125          *
1126          * x E [1, (2^16 - 1) * (2^8 - 1)]
1127          *
1128          * Substituting maximum x to the original formula (with rounding),
1129          * the maximum l is thus
1130          *
1131          * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
1132          *
1133          * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
1134          * --------------------------------------------------
1135          *
1136          * flash_strobe_length must be clamped between 1 and
1137          * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
1138          *
1139          * Then,
1140          *
1141          * flash_strobe_adjustment = ((flash_strobe_length *
1142          *      EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
1143          *
1144          * tFlash_strobe_width_ctrl = ((flash_strobe_length *
1145          *      EXTCLK freq + 10^6 - 1) / 10^6 +
1146          *      flash_strobe_adjustment - 1) / flash_strobe_adjustment
1147          */
1148         tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
1149                       1000000 + 1, ext_freq);
1150         strobe_setup->strobe_width_high_us =
1151                 clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
1152 
1153         tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
1154                         1000000 - 1), 1000000ULL);
1155         strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
1156         strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
1157                                 strobe_adjustment;
1158 
1159         rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
1160                             strobe_setup->mode);
1161         if (rval < 0)
1162                 goto out;
1163 
1164         rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
1165                             strobe_adjustment);
1166         if (rval < 0)
1167                 goto out;
1168 
1169         rval = smiapp_write(
1170                 sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
1171                 strobe_width_high_rs);
1172         if (rval < 0)
1173                 goto out;
1174 
1175         rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
1176                             strobe_setup->strobe_delay);
1177         if (rval < 0)
1178                 goto out;
1179 
1180         rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
1181                             strobe_setup->stobe_start_point);
1182         if (rval < 0)
1183                 goto out;
1184 
1185         rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
1186                             strobe_setup->trigger);
1187 
1188 out:
1189         sensor->hwcfg->strobe_setup->trigger = 0;
1190 
1191         return rval;
1192 }
1193 
1194 /* -----------------------------------------------------------------------------
1195  * Power management
1196  */
1197 
1198 static int smiapp_power_on(struct device *dev)
1199 {
1200         struct i2c_client *client = to_i2c_client(dev);
1201         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1202         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1203         /*
1204          * The sub-device related to the I2C device is always the
1205          * source one, i.e. ssds[0].
1206          */
1207         struct smiapp_sensor *sensor =
1208                 container_of(ssd, struct smiapp_sensor, ssds[0]);
1209         unsigned int sleep;
1210         int rval;
1211 
1212         rval = regulator_enable(sensor->vana);
1213         if (rval) {
1214                 dev_err(&client->dev, "failed to enable vana regulator\n");
1215                 return rval;
1216         }
1217         usleep_range(1000, 1000);
1218 
1219         rval = clk_prepare_enable(sensor->ext_clk);
1220         if (rval < 0) {
1221                 dev_dbg(&client->dev, "failed to enable xclk\n");
1222                 goto out_xclk_fail;
1223         }
1224         usleep_range(1000, 1000);
1225 
1226         gpiod_set_value(sensor->xshutdown, 1);
1227 
1228         sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
1229         usleep_range(sleep, sleep);
1230 
1231         mutex_lock(&sensor->mutex);
1232 
1233         sensor->active = true;
1234 
1235         /*
1236          * Failures to respond to the address change command have been noticed.
1237          * Those failures seem to be caused by the sensor requiring a longer
1238          * boot time than advertised. An additional 10ms delay seems to work
1239          * around the issue, but the SMIA++ I2C write retry hack makes the delay
1240          * unnecessary. The failures need to be investigated to find a proper
1241          * fix, and a delay will likely need to be added here if the I2C write
1242          * retry hack is reverted before the root cause of the boot time issue
1243          * is found.
1244          */
1245 
1246         if (sensor->hwcfg->i2c_addr_alt) {
1247                 rval = smiapp_change_cci_addr(sensor);
1248                 if (rval) {
1249                         dev_err(&client->dev, "cci address change error\n");
1250                         goto out_cci_addr_fail;
1251                 }
1252         }
1253 
1254         rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
1255                             SMIAPP_SOFTWARE_RESET);
1256         if (rval < 0) {
1257                 dev_err(&client->dev, "software reset failed\n");
1258                 goto out_cci_addr_fail;
1259         }
1260 
1261         if (sensor->hwcfg->i2c_addr_alt) {
1262                 rval = smiapp_change_cci_addr(sensor);
1263                 if (rval) {
1264                         dev_err(&client->dev, "cci address change error\n");
1265                         goto out_cci_addr_fail;
1266                 }
1267         }
1268 
1269         rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
1270                             SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
1271         if (rval) {
1272                 dev_err(&client->dev, "compression mode set failed\n");
1273                 goto out_cci_addr_fail;
1274         }
1275 
1276         rval = smiapp_write(
1277                 sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1278                 sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
1279         if (rval) {
1280                 dev_err(&client->dev, "extclk frequency set failed\n");
1281                 goto out_cci_addr_fail;
1282         }
1283 
1284         rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1285                             sensor->hwcfg->lanes - 1);
1286         if (rval) {
1287                 dev_err(&client->dev, "csi lane mode set failed\n");
1288                 goto out_cci_addr_fail;
1289         }
1290 
1291         rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
1292                             SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
1293         if (rval) {
1294                 dev_err(&client->dev, "fast standby set failed\n");
1295                 goto out_cci_addr_fail;
1296         }
1297 
1298         rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1299                             sensor->hwcfg->csi_signalling_mode);
1300         if (rval) {
1301                 dev_err(&client->dev, "csi signalling mode set failed\n");
1302                 goto out_cci_addr_fail;
1303         }
1304 
1305         /* DPHY control done by sensor based on requested link rate */
1306         rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
1307                             SMIAPP_DPHY_CTRL_UI);
1308         if (rval < 0)
1309                 goto out_cci_addr_fail;
1310 
1311         rval = smiapp_call_quirk(sensor, post_poweron);
1312         if (rval) {
1313                 dev_err(&client->dev, "post_poweron quirks failed\n");
1314                 goto out_cci_addr_fail;
1315         }
1316 
1317         /* Are we still initialising...? If not, proceed with control setup. */
1318         if (sensor->pixel_array) {
1319                 rval = __v4l2_ctrl_handler_setup(
1320                         &sensor->pixel_array->ctrl_handler);
1321                 if (rval)
1322                         goto out_cci_addr_fail;
1323 
1324                 rval = __v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
1325                 if (rval)
1326                         goto out_cci_addr_fail;
1327 
1328                 rval = smiapp_update_mode(sensor);
1329                 if (rval < 0)
1330                         goto out_cci_addr_fail;
1331         }
1332 
1333         mutex_unlock(&sensor->mutex);
1334 
1335         return 0;
1336 
1337 out_cci_addr_fail:
1338         mutex_unlock(&sensor->mutex);
1339         gpiod_set_value(sensor->xshutdown, 0);
1340         clk_disable_unprepare(sensor->ext_clk);
1341 
1342 out_xclk_fail:
1343         regulator_disable(sensor->vana);
1344 
1345         return rval;
1346 }
1347 
1348 static int smiapp_power_off(struct device *dev)
1349 {
1350         struct i2c_client *client = to_i2c_client(dev);
1351         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1352         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1353         struct smiapp_sensor *sensor =
1354                 container_of(ssd, struct smiapp_sensor, ssds[0]);
1355 
1356         mutex_lock(&sensor->mutex);
1357 
1358         /*
1359          * Currently power/clock to lens are enable/disabled separately
1360          * but they are essentially the same signals. So if the sensor is
1361          * powered off while the lens is powered on the sensor does not
1362          * really see a power off and next time the cci address change
1363          * will fail. So do a soft reset explicitly here.
1364          */
1365         if (sensor->hwcfg->i2c_addr_alt)
1366                 smiapp_write(sensor,
1367                              SMIAPP_REG_U8_SOFTWARE_RESET,
1368                              SMIAPP_SOFTWARE_RESET);
1369 
1370         sensor->active = false;
1371 
1372         mutex_unlock(&sensor->mutex);
1373 
1374         gpiod_set_value(sensor->xshutdown, 0);
1375         clk_disable_unprepare(sensor->ext_clk);
1376         usleep_range(5000, 5000);
1377         regulator_disable(sensor->vana);
1378         sensor->streaming = false;
1379 
1380         return 0;
1381 }
1382 
1383 /* -----------------------------------------------------------------------------
1384  * Video stream management
1385  */
1386 
1387 static int smiapp_start_streaming(struct smiapp_sensor *sensor)
1388 {
1389         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1390         int rval;
1391 
1392         mutex_lock(&sensor->mutex);
1393 
1394         rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
1395                             (sensor->csi_format->width << 8) |
1396                             sensor->csi_format->compressed);
1397         if (rval)
1398                 goto out;
1399 
1400         rval = smiapp_pll_configure(sensor);
1401         if (rval)
1402                 goto out;
1403 
1404         /* Analog crop start coordinates */
1405         rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
1406                             sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
1407         if (rval < 0)
1408                 goto out;
1409 
1410         rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
1411                             sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
1412         if (rval < 0)
1413                 goto out;
1414 
1415         /* Analog crop end coordinates */
1416         rval = smiapp_write(
1417                 sensor, SMIAPP_REG_U16_X_ADDR_END,
1418                 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
1419                 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
1420         if (rval < 0)
1421                 goto out;
1422 
1423         rval = smiapp_write(
1424                 sensor, SMIAPP_REG_U16_Y_ADDR_END,
1425                 sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
1426                 + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
1427         if (rval < 0)
1428                 goto out;
1429 
1430         /*
1431          * Output from pixel array, including blanking, is set using
1432          * controls below. No need to set here.
1433          */
1434 
1435         /* Digital crop */
1436         if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
1437             == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
1438                 rval = smiapp_write(
1439                         sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
1440                         sensor->scaler->crop[SMIAPP_PAD_SINK].left);
1441                 if (rval < 0)
1442                         goto out;
1443 
1444                 rval = smiapp_write(
1445                         sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
1446                         sensor->scaler->crop[SMIAPP_PAD_SINK].top);
1447                 if (rval < 0)
1448                         goto out;
1449 
1450                 rval = smiapp_write(
1451                         sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
1452                         sensor->scaler->crop[SMIAPP_PAD_SINK].width);
1453                 if (rval < 0)
1454                         goto out;
1455 
1456                 rval = smiapp_write(
1457                         sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
1458                         sensor->scaler->crop[SMIAPP_PAD_SINK].height);
1459                 if (rval < 0)
1460                         goto out;
1461         }
1462 
1463         /* Scaling */
1464         if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
1465             != SMIAPP_SCALING_CAPABILITY_NONE) {
1466                 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
1467                                     sensor->scaling_mode);
1468                 if (rval < 0)
1469                         goto out;
1470 
1471                 rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
1472                                     sensor->scale_m);
1473                 if (rval < 0)
1474                         goto out;
1475         }
1476 
1477         /* Output size from sensor */
1478         rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
1479                             sensor->src->crop[SMIAPP_PAD_SRC].width);
1480         if (rval < 0)
1481                 goto out;
1482         rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
1483                             sensor->src->crop[SMIAPP_PAD_SRC].height);
1484         if (rval < 0)
1485                 goto out;
1486 
1487         if ((sensor->limits[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY] &
1488              (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
1489               SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1490             sensor->hwcfg->strobe_setup != NULL &&
1491             sensor->hwcfg->strobe_setup->trigger != 0) {
1492                 rval = smiapp_setup_flash_strobe(sensor);
1493                 if (rval)
1494                         goto out;
1495         }
1496 
1497         rval = smiapp_call_quirk(sensor, pre_streamon);
1498         if (rval) {
1499                 dev_err(&client->dev, "pre_streamon quirks failed\n");
1500                 goto out;
1501         }
1502 
1503         rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1504                             SMIAPP_MODE_SELECT_STREAMING);
1505 
1506 out:
1507         mutex_unlock(&sensor->mutex);
1508 
1509         return rval;
1510 }
1511 
1512 static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
1513 {
1514         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1515         int rval;
1516 
1517         mutex_lock(&sensor->mutex);
1518         rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
1519                             SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
1520         if (rval)
1521                 goto out;
1522 
1523         rval = smiapp_call_quirk(sensor, post_streamoff);
1524         if (rval)
1525                 dev_err(&client->dev, "post_streamoff quirks failed\n");
1526 
1527 out:
1528         mutex_unlock(&sensor->mutex);
1529         return rval;
1530 }
1531 
1532 /* -----------------------------------------------------------------------------
1533  * V4L2 subdev video operations
1534  */
1535 
1536 static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
1537 {
1538         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1539         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
1540         int rval;
1541 
1542         if (sensor->streaming == enable)
1543                 return 0;
1544 
1545         if (enable) {
1546                 rval = pm_runtime_get_sync(&client->dev);
1547                 if (rval < 0) {
1548                         if (rval != -EBUSY && rval != -EAGAIN)
1549                                 pm_runtime_set_active(&client->dev);
1550                         pm_runtime_put(&client->dev);
1551                         return rval;
1552                 }
1553 
1554                 sensor->streaming = true;
1555 
1556                 rval = smiapp_start_streaming(sensor);
1557                 if (rval < 0)
1558                         sensor->streaming = false;
1559         } else {
1560                 rval = smiapp_stop_streaming(sensor);
1561                 sensor->streaming = false;
1562                 pm_runtime_mark_last_busy(&client->dev);
1563                 pm_runtime_put_autosuspend(&client->dev);
1564         }
1565 
1566         return rval;
1567 }
1568 
1569 static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1570                                  struct v4l2_subdev_pad_config *cfg,
1571                                  struct v4l2_subdev_mbus_code_enum *code)
1572 {
1573         struct i2c_client *client = v4l2_get_subdevdata(subdev);
1574         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1575         unsigned int i;
1576         int idx = -1;
1577         int rval = -EINVAL;
1578 
1579         mutex_lock(&sensor->mutex);
1580 
1581         dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
1582                 subdev->name, code->pad, code->index);
1583 
1584         if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
1585                 if (code->index)
1586                         goto out;
1587 
1588                 code->code = sensor->internal_csi_format->code;
1589                 rval = 0;
1590                 goto out;
1591         }
1592 
1593         for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1594                 if (sensor->mbus_frame_fmts & (1 << i))
1595                         idx++;
1596 
1597                 if (idx == code->index) {
1598                         code->code = smiapp_csi_data_formats[i].code;
1599                         dev_err(&client->dev, "found index %d, i %d, code %x\n",
1600                                 code->index, i, code->code);
1601                         rval = 0;
1602                         break;
1603                 }
1604         }
1605 
1606 out:
1607         mutex_unlock(&sensor->mutex);
1608 
1609         return rval;
1610 }
1611 
1612 static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
1613                                   unsigned int pad)
1614 {
1615         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1616 
1617         if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
1618                 return sensor->csi_format->code;
1619         else
1620                 return sensor->internal_csi_format->code;
1621 }
1622 
1623 static int __smiapp_get_format(struct v4l2_subdev *subdev,
1624                                struct v4l2_subdev_pad_config *cfg,
1625                                struct v4l2_subdev_format *fmt)
1626 {
1627         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1628 
1629         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1630                 fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
1631                                                           fmt->pad);
1632         } else {
1633                 struct v4l2_rect *r;
1634 
1635                 if (fmt->pad == ssd->source_pad)
1636                         r = &ssd->crop[ssd->source_pad];
1637                 else
1638                         r = &ssd->sink_fmt;
1639 
1640                 fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1641                 fmt->format.width = r->width;
1642                 fmt->format.height = r->height;
1643                 fmt->format.field = V4L2_FIELD_NONE;
1644         }
1645 
1646         return 0;
1647 }
1648 
1649 static int smiapp_get_format(struct v4l2_subdev *subdev,
1650                              struct v4l2_subdev_pad_config *cfg,
1651                              struct v4l2_subdev_format *fmt)
1652 {
1653         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1654         int rval;
1655 
1656         mutex_lock(&sensor->mutex);
1657         rval = __smiapp_get_format(subdev, cfg, fmt);
1658         mutex_unlock(&sensor->mutex);
1659 
1660         return rval;
1661 }
1662 
1663 static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1664                                     struct v4l2_subdev_pad_config *cfg,
1665                                     struct v4l2_rect **crops,
1666                                     struct v4l2_rect **comps, int which)
1667 {
1668         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1669         unsigned int i;
1670 
1671         if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1672                 if (crops)
1673                         for (i = 0; i < subdev->entity.num_pads; i++)
1674                                 crops[i] = &ssd->crop[i];
1675                 if (comps)
1676                         *comps = &ssd->compose;
1677         } else {
1678                 if (crops) {
1679                         for (i = 0; i < subdev->entity.num_pads; i++) {
1680                                 crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
1681                                 BUG_ON(!crops[i]);
1682                         }
1683                 }
1684                 if (comps) {
1685                         *comps = v4l2_subdev_get_try_compose(subdev, cfg,
1686                                                              SMIAPP_PAD_SINK);
1687                         BUG_ON(!*comps);
1688                 }
1689         }
1690 }
1691 
1692 /* Changes require propagation only on sink pad. */
1693 static void smiapp_propagate(struct v4l2_subdev *subdev,
1694                              struct v4l2_subdev_pad_config *cfg, int which,
1695                              int target)
1696 {
1697         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1698         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1699         struct v4l2_rect *comp, *crops[SMIAPP_PADS];
1700 
1701         smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
1702 
1703         switch (target) {
1704         case V4L2_SEL_TGT_CROP:
1705                 comp->width = crops[SMIAPP_PAD_SINK]->width;
1706                 comp->height = crops[SMIAPP_PAD_SINK]->height;
1707                 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1708                         if (ssd == sensor->scaler) {
1709                                 sensor->scale_m =
1710                                         sensor->limits[
1711                                                 SMIAPP_LIMIT_SCALER_N_MIN];
1712                                 sensor->scaling_mode =
1713                                         SMIAPP_SCALING_MODE_NONE;
1714                         } else if (ssd == sensor->binner) {
1715                                 sensor->binning_horizontal = 1;
1716                                 sensor->binning_vertical = 1;
1717                         }
1718                 }
1719                 /* Fall through */
1720         case V4L2_SEL_TGT_COMPOSE:
1721                 *crops[SMIAPP_PAD_SRC] = *comp;
1722                 break;
1723         default:
1724                 BUG();
1725         }
1726 }
1727 
1728 static const struct smiapp_csi_data_format
1729 *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
1730 {
1731         unsigned int i;
1732 
1733         for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
1734                 if (sensor->mbus_frame_fmts & (1 << i)
1735                     && smiapp_csi_data_formats[i].code == code)
1736                         return &smiapp_csi_data_formats[i];
1737         }
1738 
1739         return sensor->csi_format;
1740 }
1741 
1742 static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1743                                     struct v4l2_subdev_pad_config *cfg,
1744                                     struct v4l2_subdev_format *fmt)
1745 {
1746         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1747         const struct smiapp_csi_data_format *csi_format,
1748                 *old_csi_format = sensor->csi_format;
1749         unsigned long *valid_link_freqs;
1750         u32 code = fmt->format.code;
1751         unsigned int i;
1752         int rval;
1753 
1754         rval = __smiapp_get_format(subdev, cfg, fmt);
1755         if (rval)
1756                 return rval;
1757 
1758         /*
1759          * Media bus code is changeable on src subdev's source pad. On
1760          * other source pads we just get format here.
1761          */
1762         if (subdev != &sensor->src->sd)
1763                 return 0;
1764 
1765         csi_format = smiapp_validate_csi_data_format(sensor, code);
1766 
1767         fmt->format.code = csi_format->code;
1768 
1769         if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1770                 return 0;
1771 
1772         sensor->csi_format = csi_format;
1773 
1774         if (csi_format->width != old_csi_format->width)
1775                 for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1776                         __v4l2_ctrl_modify_range(
1777                                 sensor->test_data[i], 0,
1778                                 (1 << csi_format->width) - 1, 1, 0);
1779 
1780         if (csi_format->compressed == old_csi_format->compressed)
1781                 return 0;
1782 
1783         valid_link_freqs =
1784                 &sensor->valid_link_freqs[sensor->csi_format->compressed
1785                                           - sensor->compressed_min_bpp];
1786 
1787         __v4l2_ctrl_modify_range(
1788                 sensor->link_freq, 0,
1789                 __fls(*valid_link_freqs), ~*valid_link_freqs,
1790                 __ffs(*valid_link_freqs));
1791 
1792         return smiapp_pll_update(sensor);
1793 }
1794 
1795 static int smiapp_set_format(struct v4l2_subdev *subdev,
1796                              struct v4l2_subdev_pad_config *cfg,
1797                              struct v4l2_subdev_format *fmt)
1798 {
1799         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1800         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
1801         struct v4l2_rect *crops[SMIAPP_PADS];
1802 
1803         mutex_lock(&sensor->mutex);
1804 
1805         if (fmt->pad == ssd->source_pad) {
1806                 int rval;
1807 
1808                 rval = smiapp_set_format_source(subdev, cfg, fmt);
1809 
1810                 mutex_unlock(&sensor->mutex);
1811 
1812                 return rval;
1813         }
1814 
1815         /* Sink pad. Width and height are changeable here. */
1816         fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
1817         fmt->format.width &= ~1;
1818         fmt->format.height &= ~1;
1819         fmt->format.field = V4L2_FIELD_NONE;
1820 
1821         fmt->format.width =
1822                 clamp(fmt->format.width,
1823                       sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
1824                       sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
1825         fmt->format.height =
1826                 clamp(fmt->format.height,
1827                       sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
1828                       sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
1829 
1830         smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
1831 
1832         crops[ssd->sink_pad]->left = 0;
1833         crops[ssd->sink_pad]->top = 0;
1834         crops[ssd->sink_pad]->width = fmt->format.width;
1835         crops[ssd->sink_pad]->height = fmt->format.height;
1836         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1837                 ssd->sink_fmt = *crops[ssd->sink_pad];
1838         smiapp_propagate(subdev, cfg, fmt->which,
1839                          V4L2_SEL_TGT_CROP);
1840 
1841         mutex_unlock(&sensor->mutex);
1842 
1843         return 0;
1844 }
1845 
1846 /*
1847  * Calculate goodness of scaled image size compared to expected image
1848  * size and flags provided.
1849  */
1850 #define SCALING_GOODNESS                100000
1851 #define SCALING_GOODNESS_EXTREME        100000000
1852 static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
1853                             int h, int ask_h, u32 flags)
1854 {
1855         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1856         struct i2c_client *client = v4l2_get_subdevdata(subdev);
1857         int val = 0;
1858 
1859         w &= ~1;
1860         ask_w &= ~1;
1861         h &= ~1;
1862         ask_h &= ~1;
1863 
1864         if (flags & V4L2_SEL_FLAG_GE) {
1865                 if (w < ask_w)
1866                         val -= SCALING_GOODNESS;
1867                 if (h < ask_h)
1868                         val -= SCALING_GOODNESS;
1869         }
1870 
1871         if (flags & V4L2_SEL_FLAG_LE) {
1872                 if (w > ask_w)
1873                         val -= SCALING_GOODNESS;
1874                 if (h > ask_h)
1875                         val -= SCALING_GOODNESS;
1876         }
1877 
1878         val -= abs(w - ask_w);
1879         val -= abs(h - ask_h);
1880 
1881         if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
1882                 val -= SCALING_GOODNESS_EXTREME;
1883 
1884         dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
1885                 w, ask_w, h, ask_h, val);
1886 
1887         return val;
1888 }
1889 
1890 static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1891                                       struct v4l2_subdev_pad_config *cfg,
1892                                       struct v4l2_subdev_selection *sel,
1893                                       struct v4l2_rect **crops,
1894                                       struct v4l2_rect *comp)
1895 {
1896         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1897         unsigned int i;
1898         unsigned int binh = 1, binv = 1;
1899         int best = scaling_goodness(
1900                 subdev,
1901                 crops[SMIAPP_PAD_SINK]->width, sel->r.width,
1902                 crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
1903 
1904         for (i = 0; i < sensor->nbinning_subtypes; i++) {
1905                 int this = scaling_goodness(
1906                         subdev,
1907                         crops[SMIAPP_PAD_SINK]->width
1908                         / sensor->binning_subtypes[i].horizontal,
1909                         sel->r.width,
1910                         crops[SMIAPP_PAD_SINK]->height
1911                         / sensor->binning_subtypes[i].vertical,
1912                         sel->r.height, sel->flags);
1913 
1914                 if (this > best) {
1915                         binh = sensor->binning_subtypes[i].horizontal;
1916                         binv = sensor->binning_subtypes[i].vertical;
1917                         best = this;
1918                 }
1919         }
1920         if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1921                 sensor->binning_vertical = binv;
1922                 sensor->binning_horizontal = binh;
1923         }
1924 
1925         sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
1926         sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
1927 }
1928 
1929 /*
1930  * Calculate best scaling ratio and mode for given output resolution.
1931  *
1932  * Try all of these: horizontal ratio, vertical ratio and smallest
1933  * size possible (horizontally).
1934  *
1935  * Also try whether horizontal scaler or full scaler gives a better
1936  * result.
1937  */
1938 static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1939                                       struct v4l2_subdev_pad_config *cfg,
1940                                       struct v4l2_subdev_selection *sel,
1941                                       struct v4l2_rect **crops,
1942                                       struct v4l2_rect *comp)
1943 {
1944         struct i2c_client *client = v4l2_get_subdevdata(subdev);
1945         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1946         u32 min, max, a, b, max_m;
1947         u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
1948         int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
1949         u32 try[4];
1950         u32 ntry = 0;
1951         unsigned int i;
1952         int best = INT_MIN;
1953 
1954         sel->r.width = min_t(unsigned int, sel->r.width,
1955                              crops[SMIAPP_PAD_SINK]->width);
1956         sel->r.height = min_t(unsigned int, sel->r.height,
1957                               crops[SMIAPP_PAD_SINK]->height);
1958 
1959         a = crops[SMIAPP_PAD_SINK]->width
1960                 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
1961         b = crops[SMIAPP_PAD_SINK]->height
1962                 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
1963         max_m = crops[SMIAPP_PAD_SINK]->width
1964                 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
1965                 / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
1966 
1967         a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1968                   sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1969         b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1970                   sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1971         max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
1972                       sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
1973 
1974         dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
1975 
1976         min = min(max_m, min(a, b));
1977         max = min(max_m, max(a, b));
1978 
1979         try[ntry] = min;
1980         ntry++;
1981         if (min != max) {
1982                 try[ntry] = max;
1983                 ntry++;
1984         }
1985         if (max != max_m) {
1986                 try[ntry] = min + 1;
1987                 ntry++;
1988                 if (min != max) {
1989                         try[ntry] = max + 1;
1990                         ntry++;
1991                 }
1992         }
1993 
1994         for (i = 0; i < ntry; i++) {
1995                 int this = scaling_goodness(
1996                         subdev,
1997                         crops[SMIAPP_PAD_SINK]->width
1998                         / try[i]
1999                         * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2000                         sel->r.width,
2001                         crops[SMIAPP_PAD_SINK]->height,
2002                         sel->r.height,
2003                         sel->flags);
2004 
2005                 dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
2006 
2007                 if (this > best) {
2008                         scale_m = try[i];
2009                         mode = SMIAPP_SCALING_MODE_HORIZONTAL;
2010                         best = this;
2011                 }
2012 
2013                 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2014                     == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
2015                         continue;
2016 
2017                 this = scaling_goodness(
2018                         subdev, crops[SMIAPP_PAD_SINK]->width
2019                         / try[i]
2020                         * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2021                         sel->r.width,
2022                         crops[SMIAPP_PAD_SINK]->height
2023                         / try[i]
2024                         * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
2025                         sel->r.height,
2026                         sel->flags);
2027 
2028                 if (this > best) {
2029                         scale_m = try[i];
2030                         mode = SMIAPP_SCALING_MODE_BOTH;
2031                         best = this;
2032                 }
2033         }
2034 
2035         sel->r.width =
2036                 (crops[SMIAPP_PAD_SINK]->width
2037                  / scale_m
2038                  * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
2039         if (mode == SMIAPP_SCALING_MODE_BOTH)
2040                 sel->r.height =
2041                         (crops[SMIAPP_PAD_SINK]->height
2042                          / scale_m
2043                          * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
2044                         & ~1;
2045         else
2046                 sel->r.height = crops[SMIAPP_PAD_SINK]->height;
2047 
2048         if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2049                 sensor->scale_m = scale_m;
2050                 sensor->scaling_mode = mode;
2051         }
2052 }
2053 /* We're only called on source pads. This function sets scaling. */
2054 static int smiapp_set_compose(struct v4l2_subdev *subdev,
2055                               struct v4l2_subdev_pad_config *cfg,
2056                               struct v4l2_subdev_selection *sel)
2057 {
2058         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2059         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2060         struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2061 
2062         smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2063 
2064         sel->r.top = 0;
2065         sel->r.left = 0;
2066 
2067         if (ssd == sensor->binner)
2068                 smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
2069         else
2070                 smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
2071 
2072         *comp = sel->r;
2073         smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
2074 
2075         if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
2076                 return smiapp_update_mode(sensor);
2077 
2078         return 0;
2079 }
2080 
2081 static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
2082                                   struct v4l2_subdev_selection *sel)
2083 {
2084         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2085         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2086 
2087         /* We only implement crop in three places. */
2088         switch (sel->target) {
2089         case V4L2_SEL_TGT_CROP:
2090         case V4L2_SEL_TGT_CROP_BOUNDS:
2091                 if (ssd == sensor->pixel_array
2092                     && sel->pad == SMIAPP_PA_PAD_SRC)
2093                         return 0;
2094                 if (ssd == sensor->src
2095                     && sel->pad == SMIAPP_PAD_SRC)
2096                         return 0;
2097                 if (ssd == sensor->scaler
2098                     && sel->pad == SMIAPP_PAD_SINK
2099                     && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
2100                     == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
2101                         return 0;
2102                 return -EINVAL;
2103         case V4L2_SEL_TGT_NATIVE_SIZE:
2104                 if (ssd == sensor->pixel_array
2105                     && sel->pad == SMIAPP_PA_PAD_SRC)
2106                         return 0;
2107                 return -EINVAL;
2108         case V4L2_SEL_TGT_COMPOSE:
2109         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2110                 if (sel->pad == ssd->source_pad)
2111                         return -EINVAL;
2112                 if (ssd == sensor->binner)
2113                         return 0;
2114                 if (ssd == sensor->scaler
2115                     && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
2116                     != SMIAPP_SCALING_CAPABILITY_NONE)
2117                         return 0;
2118                 /* Fall through */
2119         default:
2120                 return -EINVAL;
2121         }
2122 }
2123 
2124 static int smiapp_set_crop(struct v4l2_subdev *subdev,
2125                            struct v4l2_subdev_pad_config *cfg,
2126                            struct v4l2_subdev_selection *sel)
2127 {
2128         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2129         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2130         struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
2131         struct v4l2_rect _r;
2132 
2133         smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
2134 
2135         if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2136                 if (sel->pad == ssd->sink_pad)
2137                         src_size = &ssd->sink_fmt;
2138                 else
2139                         src_size = &ssd->compose;
2140         } else {
2141                 if (sel->pad == ssd->sink_pad) {
2142                         _r.left = 0;
2143                         _r.top = 0;
2144                         _r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2145                                 ->width;
2146                         _r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
2147                                 ->height;
2148                         src_size = &_r;
2149                 } else {
2150                         src_size = v4l2_subdev_get_try_compose(
2151                                 subdev, cfg, ssd->sink_pad);
2152                 }
2153         }
2154 
2155         if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
2156                 sel->r.left = 0;
2157                 sel->r.top = 0;
2158         }
2159 
2160         sel->r.width = min(sel->r.width, src_size->width);
2161         sel->r.height = min(sel->r.height, src_size->height);
2162 
2163         sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
2164         sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
2165 
2166         *crops[sel->pad] = sel->r;
2167 
2168         if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2169                 smiapp_propagate(subdev, cfg, sel->which,
2170                                  V4L2_SEL_TGT_CROP);
2171 
2172         return 0;
2173 }
2174 
2175 static void smiapp_get_native_size(struct smiapp_subdev *ssd,
2176                                     struct v4l2_rect *r)
2177 {
2178         r->top = 0;
2179         r->left = 0;
2180         r->width = ssd->sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
2181         r->height = ssd->sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
2182 }
2183 
2184 static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2185                                   struct v4l2_subdev_pad_config *cfg,
2186                                   struct v4l2_subdev_selection *sel)
2187 {
2188         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2189         struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
2190         struct v4l2_rect *comp, *crops[SMIAPP_PADS];
2191         struct v4l2_rect sink_fmt;
2192         int ret;
2193 
2194         ret = __smiapp_sel_supported(subdev, sel);
2195         if (ret)
2196                 return ret;
2197 
2198         smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
2199 
2200         if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
2201                 sink_fmt = ssd->sink_fmt;
2202         } else {
2203                 struct v4l2_mbus_framefmt *fmt =
2204                         v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
2205 
2206                 sink_fmt.left = 0;
2207                 sink_fmt.top = 0;
2208                 sink_fmt.width = fmt->width;
2209                 sink_fmt.height = fmt->height;
2210         }
2211 
2212         switch (sel->target) {
2213         case V4L2_SEL_TGT_CROP_BOUNDS:
2214         case V4L2_SEL_TGT_NATIVE_SIZE:
2215                 if (ssd == sensor->pixel_array)
2216                         smiapp_get_native_size(ssd, &sel->r);
2217                 else if (sel->pad == ssd->sink_pad)
2218                         sel->r = sink_fmt;
2219                 else
2220                         sel->r = *comp;
2221                 break;
2222         case V4L2_SEL_TGT_CROP:
2223         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2224                 sel->r = *crops[sel->pad];
2225                 break;
2226         case V4L2_SEL_TGT_COMPOSE:
2227                 sel->r = *comp;
2228                 break;
2229         }
2230 
2231         return 0;
2232 }
2233 
2234 static int smiapp_get_selection(struct v4l2_subdev *subdev,
2235                                 struct v4l2_subdev_pad_config *cfg,
2236                                 struct v4l2_subdev_selection *sel)
2237 {
2238         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2239         int rval;
2240 
2241         mutex_lock(&sensor->mutex);
2242         rval = __smiapp_get_selection(subdev, cfg, sel);
2243         mutex_unlock(&sensor->mutex);
2244 
2245         return rval;
2246 }
2247 static int smiapp_set_selection(struct v4l2_subdev *subdev,
2248                                 struct v4l2_subdev_pad_config *cfg,
2249                                 struct v4l2_subdev_selection *sel)
2250 {
2251         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2252         int ret;
2253 
2254         ret = __smiapp_sel_supported(subdev, sel);
2255         if (ret)
2256                 return ret;
2257 
2258         mutex_lock(&sensor->mutex);
2259 
2260         sel->r.left = max(0, sel->r.left & ~1);
2261         sel->r.top = max(0, sel->r.top & ~1);
2262         sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
2263         sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
2264 
2265         sel->r.width = max_t(unsigned int,
2266                              sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
2267                              sel->r.width);
2268         sel->r.height = max_t(unsigned int,
2269                               sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
2270                               sel->r.height);
2271 
2272         switch (sel->target) {
2273         case V4L2_SEL_TGT_CROP:
2274                 ret = smiapp_set_crop(subdev, cfg, sel);
2275                 break;
2276         case V4L2_SEL_TGT_COMPOSE:
2277                 ret = smiapp_set_compose(subdev, cfg, sel);
2278                 break;
2279         default:
2280                 ret = -EINVAL;
2281         }
2282 
2283         mutex_unlock(&sensor->mutex);
2284         return ret;
2285 }
2286 
2287 static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
2288 {
2289         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2290 
2291         *frames = sensor->frame_skip;
2292         return 0;
2293 }
2294 
2295 static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
2296 {
2297         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2298 
2299         *lines = sensor->image_start;
2300 
2301         return 0;
2302 }
2303 
2304 /* -----------------------------------------------------------------------------
2305  * sysfs attributes
2306  */
2307 
2308 static ssize_t
2309 smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
2310                       char *buf)
2311 {
2312         struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2313         struct i2c_client *client = v4l2_get_subdevdata(subdev);
2314         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2315         unsigned int nbytes;
2316 
2317         if (!sensor->dev_init_done)
2318                 return -EBUSY;
2319 
2320         if (!sensor->nvm_size) {
2321                 int rval;
2322 
2323                 /* NVM not read yet - read it now */
2324                 sensor->nvm_size = sensor->hwcfg->nvm_size;
2325 
2326                 rval = pm_runtime_get_sync(&client->dev);
2327                 if (rval < 0) {
2328                         if (rval != -EBUSY && rval != -EAGAIN)
2329                                 pm_runtime_set_active(&client->dev);
2330                         pm_runtime_put(&client->dev);
2331                         return -ENODEV;
2332                 }
2333 
2334                 if (smiapp_read_nvm(sensor, sensor->nvm)) {
2335                         dev_err(&client->dev, "nvm read failed\n");
2336                         return -ENODEV;
2337                 }
2338 
2339                 pm_runtime_mark_last_busy(&client->dev);
2340                 pm_runtime_put_autosuspend(&client->dev);
2341         }
2342         /*
2343          * NVM is still way below a PAGE_SIZE, so we can safely
2344          * assume this for now.
2345          */
2346         nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
2347         memcpy(buf, sensor->nvm, nbytes);
2348 
2349         return nbytes;
2350 }
2351 static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
2352 
2353 static ssize_t
2354 smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
2355                         char *buf)
2356 {
2357         struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
2358         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2359         struct smiapp_module_info *minfo = &sensor->minfo;
2360 
2361         return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
2362                         minfo->manufacturer_id, minfo->model_id,
2363                         minfo->revision_number_major) + 1;
2364 }
2365 
2366 static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
2367 
2368 /* -----------------------------------------------------------------------------
2369  * V4L2 subdev core operations
2370  */
2371 
2372 static int smiapp_identify_module(struct smiapp_sensor *sensor)
2373 {
2374         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2375         struct smiapp_module_info *minfo = &sensor->minfo;
2376         unsigned int i;
2377         int rval = 0;
2378 
2379         minfo->name = SMIAPP_NAME;
2380 
2381         /* Module info */
2382         rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
2383                                  &minfo->manufacturer_id);
2384         if (!rval)
2385                 rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
2386                                          &minfo->model_id);
2387         if (!rval)
2388                 rval = smiapp_read_8only(sensor,
2389                                          SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
2390                                          &minfo->revision_number_major);
2391         if (!rval)
2392                 rval = smiapp_read_8only(sensor,
2393                                          SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
2394                                          &minfo->revision_number_minor);
2395         if (!rval)
2396                 rval = smiapp_read_8only(sensor,
2397                                          SMIAPP_REG_U8_MODULE_DATE_YEAR,
2398                                          &minfo->module_year);
2399         if (!rval)
2400                 rval = smiapp_read_8only(sensor,
2401                                          SMIAPP_REG_U8_MODULE_DATE_MONTH,
2402                                          &minfo->module_month);
2403         if (!rval)
2404                 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
2405                                          &minfo->module_day);
2406 
2407         /* Sensor info */
2408         if (!rval)
2409                 rval = smiapp_read_8only(sensor,
2410                                          SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
2411                                          &minfo->sensor_manufacturer_id);
2412         if (!rval)
2413                 rval = smiapp_read_8only(sensor,
2414                                          SMIAPP_REG_U16_SENSOR_MODEL_ID,
2415                                          &minfo->sensor_model_id);
2416         if (!rval)
2417                 rval = smiapp_read_8only(sensor,
2418                                          SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
2419                                          &minfo->sensor_revision_number);
2420         if (!rval)
2421                 rval = smiapp_read_8only(sensor,
2422                                          SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
2423                                          &minfo->sensor_firmware_version);
2424 
2425         /* SMIA */
2426         if (!rval)
2427                 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
2428                                          &minfo->smia_version);
2429         if (!rval)
2430                 rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
2431                                          &minfo->smiapp_version);
2432 
2433         if (rval) {
2434                 dev_err(&client->dev, "sensor detection failed\n");
2435                 return -ENODEV;
2436         }
2437 
2438         dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
2439                 minfo->manufacturer_id, minfo->model_id);
2440 
2441         dev_dbg(&client->dev,
2442                 "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
2443                 minfo->revision_number_major, minfo->revision_number_minor,
2444                 minfo->module_year, minfo->module_month, minfo->module_day);
2445 
2446         dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
2447                 minfo->sensor_manufacturer_id, minfo->sensor_model_id);
2448 
2449         dev_dbg(&client->dev,
2450                 "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
2451                 minfo->sensor_revision_number, minfo->sensor_firmware_version);
2452 
2453         dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
2454                 minfo->smia_version, minfo->smiapp_version);
2455 
2456         /*
2457          * Some modules have bad data in the lvalues below. Hope the
2458          * rvalues have better stuff. The lvalues are module
2459          * parameters whereas the rvalues are sensor parameters.
2460          */
2461         if (!minfo->manufacturer_id && !minfo->model_id) {
2462                 minfo->manufacturer_id = minfo->sensor_manufacturer_id;
2463                 minfo->model_id = minfo->sensor_model_id;
2464                 minfo->revision_number_major = minfo->sensor_revision_number;
2465         }
2466 
2467         for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
2468                 if (smiapp_module_idents[i].manufacturer_id
2469                     != minfo->manufacturer_id)
2470                         continue;
2471                 if (smiapp_module_idents[i].model_id != minfo->model_id)
2472                         continue;
2473                 if (smiapp_module_idents[i].flags
2474                     & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
2475                         if (smiapp_module_idents[i].revision_number_major
2476                             < minfo->revision_number_major)
2477                                 continue;
2478                 } else {
2479                         if (smiapp_module_idents[i].revision_number_major
2480                             != minfo->revision_number_major)
2481                                 continue;
2482                 }
2483 
2484                 minfo->name = smiapp_module_idents[i].name;
2485                 minfo->quirk = smiapp_module_idents[i].quirk;
2486                 break;
2487         }
2488 
2489         if (i >= ARRAY_SIZE(smiapp_module_idents))
2490                 dev_warn(&client->dev,
2491                          "no quirks for this module; let's hope it's fully compliant\n");
2492 
2493         dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
2494                 minfo->name, minfo->manufacturer_id, minfo->model_id,
2495                 minfo->revision_number_major);
2496 
2497         return 0;
2498 }
2499 
2500 static const struct v4l2_subdev_ops smiapp_ops;
2501 static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
2502 static const struct media_entity_operations smiapp_entity_ops;
2503 
2504 static int smiapp_register_subdev(struct smiapp_sensor *sensor,
2505                                   struct smiapp_subdev *ssd,
2506                                   struct smiapp_subdev *sink_ssd,
2507                                   u16 source_pad, u16 sink_pad, u32 link_flags)
2508 {
2509         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2510         int rval;
2511 
2512         if (!sink_ssd)
2513                 return 0;
2514 
2515         rval = media_entity_pads_init(&ssd->sd.entity,
2516                                       ssd->npads, ssd->pads);
2517         if (rval) {
2518                 dev_err(&client->dev,
2519                         "media_entity_pads_init failed\n");
2520                 return rval;
2521         }
2522 
2523         rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
2524                                            &ssd->sd);
2525         if (rval) {
2526                 dev_err(&client->dev,
2527                         "v4l2_device_register_subdev failed\n");
2528                 return rval;
2529         }
2530 
2531         rval = media_create_pad_link(&ssd->sd.entity, source_pad,
2532                                      &sink_ssd->sd.entity, sink_pad,
2533                                      link_flags);
2534         if (rval) {
2535                 dev_err(&client->dev,
2536                         "media_create_pad_link failed\n");
2537                 v4l2_device_unregister_subdev(&ssd->sd);
2538                 return rval;
2539         }
2540 
2541         return 0;
2542 }
2543 
2544 static void smiapp_unregistered(struct v4l2_subdev *subdev)
2545 {
2546         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2547         unsigned int i;
2548 
2549         for (i = 1; i < sensor->ssds_used; i++)
2550                 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
2551 }
2552 
2553 static int smiapp_registered(struct v4l2_subdev *subdev)
2554 {
2555         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2556         int rval;
2557 
2558         if (sensor->scaler) {
2559                 rval = smiapp_register_subdev(
2560                         sensor, sensor->binner, sensor->scaler,
2561                         SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
2562                         MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2563                 if (rval < 0)
2564                         return rval;
2565         }
2566 
2567         rval = smiapp_register_subdev(
2568                 sensor, sensor->pixel_array, sensor->binner,
2569                 SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
2570                 MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2571         if (rval)
2572                 goto out_err;
2573 
2574         return 0;
2575 
2576 out_err:
2577         smiapp_unregistered(subdev);
2578 
2579         return rval;
2580 }
2581 
2582 static void smiapp_cleanup(struct smiapp_sensor *sensor)
2583 {
2584         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2585 
2586         device_remove_file(&client->dev, &dev_attr_nvm);
2587         device_remove_file(&client->dev, &dev_attr_ident);
2588 
2589         smiapp_free_controls(sensor);
2590 }
2591 
2592 static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2593                                  struct smiapp_subdev *ssd, const char *name,
2594                                  unsigned short num_pads)
2595 {
2596         struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
2597 
2598         if (!ssd)
2599                 return;
2600 
2601         if (ssd != sensor->src)
2602                 v4l2_subdev_init(&ssd->sd, &smiapp_ops);
2603 
2604         ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2605         ssd->sensor = sensor;
2606 
2607         ssd->npads = num_pads;
2608         ssd->source_pad = num_pads - 1;
2609 
2610         v4l2_i2c_subdev_set_name(&ssd->sd, client, sensor->minfo.name, name);
2611 
2612         smiapp_get_native_size(ssd, &ssd->sink_fmt);
2613 
2614         ssd->compose.width = ssd->sink_fmt.width;
2615         ssd->compose.height = ssd->sink_fmt.height;
2616         ssd->crop[ssd->source_pad] = ssd->compose;
2617         ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
2618         if (ssd != sensor->pixel_array) {
2619                 ssd->crop[ssd->sink_pad] = ssd->compose;
2620                 ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
2621         }
2622 
2623         ssd->sd.entity.ops = &smiapp_entity_ops;
2624 
2625         if (ssd == sensor->src)
2626                 return;
2627 
2628         ssd->sd.internal_ops = &smiapp_internal_ops;
2629         ssd->sd.owner = THIS_MODULE;
2630         ssd->sd.dev = &client->dev;
2631         v4l2_set_subdevdata(&ssd->sd, client);
2632 }
2633 
2634 static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2635 {
2636         struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
2637         struct smiapp_sensor *sensor = ssd->sensor;
2638         unsigned int i;
2639 
2640         mutex_lock(&sensor->mutex);
2641 
2642         for (i = 0; i < ssd->npads; i++) {
2643                 struct v4l2_mbus_framefmt *try_fmt =
2644                         v4l2_subdev_get_try_format(sd, fh->pad, i);
2645                 struct v4l2_rect *try_crop =
2646                         v4l2_subdev_get_try_crop(sd, fh->pad, i);
2647                 struct v4l2_rect *try_comp;
2648 
2649                 smiapp_get_native_size(ssd, try_crop);
2650 
2651                 try_fmt->width = try_crop->width;
2652                 try_fmt->height = try_crop->height;
2653                 try_fmt->code = sensor->internal_csi_format->code;
2654                 try_fmt->field = V4L2_FIELD_NONE;
2655 
2656                 if (ssd != sensor->pixel_array)
2657                         continue;
2658 
2659                 try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
2660                 *try_comp = *try_crop;
2661         }
2662 
2663         mutex_unlock(&sensor->mutex);
2664 
2665         return 0;
2666 }
2667 
2668 static const struct v4l2_subdev_video_ops smiapp_video_ops = {
2669         .s_stream = smiapp_set_stream,
2670 };
2671 
2672 static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
2673         .enum_mbus_code = smiapp_enum_mbus_code,
2674         .get_fmt = smiapp_get_format,
2675         .set_fmt = smiapp_set_format,
2676         .get_selection = smiapp_get_selection,
2677         .set_selection = smiapp_set_selection,
2678 };
2679 
2680 static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
2681         .g_skip_frames = smiapp_get_skip_frames,
2682         .g_skip_top_lines = smiapp_get_skip_top_lines,
2683 };
2684 
2685 static const struct v4l2_subdev_ops smiapp_ops = {
2686         .video = &smiapp_video_ops,
2687         .pad = &smiapp_pad_ops,
2688         .sensor = &smiapp_sensor_ops,
2689 };
2690 
2691 static const struct media_entity_operations smiapp_entity_ops = {
2692         .link_validate = v4l2_subdev_link_validate,
2693 };
2694 
2695 static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
2696         .registered = smiapp_registered,
2697         .unregistered = smiapp_unregistered,
2698         .open = smiapp_open,
2699 };
2700 
2701 static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
2702         .open = smiapp_open,
2703 };
2704 
2705 /* -----------------------------------------------------------------------------
2706  * I2C Driver
2707  */
2708 
2709 static int __maybe_unused smiapp_suspend(struct device *dev)
2710 {
2711         struct i2c_client *client = to_i2c_client(dev);
2712         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2713         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2714         bool streaming = sensor->streaming;
2715         int rval;
2716 
2717         rval = pm_runtime_get_sync(dev);
2718         if (rval < 0) {
2719                 if (rval != -EBUSY && rval != -EAGAIN)
2720                         pm_runtime_set_active(&client->dev);
2721                 pm_runtime_put(dev);
2722                 return -EAGAIN;
2723         }
2724 
2725         if (sensor->streaming)
2726                 smiapp_stop_streaming(sensor);
2727 
2728         /* save state for resume */
2729         sensor->streaming = streaming;
2730 
2731         return 0;
2732 }
2733 
2734 static int __maybe_unused smiapp_resume(struct device *dev)
2735 {
2736         struct i2c_client *client = to_i2c_client(dev);
2737         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
2738         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2739         int rval = 0;
2740 
2741         pm_runtime_put(dev);
2742 
2743         if (sensor->streaming)
2744                 rval = smiapp_start_streaming(sensor);
2745 
2746         return rval;
2747 }
2748 
2749 static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2750 {
2751         struct smiapp_hwconfig *hwcfg;
2752         struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2753         struct fwnode_handle *ep;
2754         struct fwnode_handle *fwnode = dev_fwnode(dev);
2755         u32 rotation;
2756         int i;
2757         int rval;
2758 
2759         if (!fwnode)
2760                 return dev->platform_data;
2761 
2762         ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
2763         if (!ep)
2764                 return NULL;
2765 
2766         bus_cfg.bus_type = V4L2_MBUS_CSI2_DPHY;
2767         rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2768         if (rval == -ENXIO) {
2769                 bus_cfg = (struct v4l2_fwnode_endpoint)
2770                         { .bus_type = V4L2_MBUS_CCP2 };
2771                 rval = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
2772         }
2773         if (rval)
2774                 goto out_err;
2775 
2776         hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
2777         if (!hwcfg)
2778                 goto out_err;
2779 
2780         switch (bus_cfg.bus_type) {
2781         case V4L2_MBUS_CSI2_DPHY:
2782                 hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2783                 hwcfg->lanes = bus_cfg.bus.mipi_csi2.num_data_lanes;
2784                 break;
2785         case V4L2_MBUS_CCP2:
2786                 hwcfg->csi_signalling_mode = (bus_cfg.bus.mipi_csi1.strobe) ?
2787                 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_STROBE :
2788                 SMIAPP_CSI_SIGNALLING_MODE_CCP2_DATA_CLOCK;
2789                 hwcfg->lanes = 1;
2790                 break;
2791         default:
2792                 dev_err(dev, "unsupported bus %u\n", bus_cfg.bus_type);
2793                 goto out_err;
2794         }
2795 
2796         dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2797 
2798         rval = fwnode_property_read_u32(fwnode, "rotation", &rotation);
2799         if (!rval) {
2800                 switch (rotation) {
2801                 case 180:
2802                         hwcfg->module_board_orient =
2803                                 SMIAPP_MODULE_BOARD_ORIENT_180;
2804                         /* Fall through */
2805                 case 0:
2806                         break;
2807                 default:
2808                         dev_err(dev, "invalid rotation %u\n", rotation);
2809                         goto out_err;
2810                 }
2811         }
2812 
2813         /* NVM size is not mandatory */
2814         fwnode_property_read_u32(fwnode, "nokia,nvm-size", &hwcfg->nvm_size);
2815 
2816         rval = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
2817                                         &hwcfg->ext_clk);
2818         if (rval)
2819                 dev_info(dev, "can't get clock-frequency\n");
2820 
2821         dev_dbg(dev, "nvm %d, clk %d, mode %d\n",
2822                 hwcfg->nvm_size, hwcfg->ext_clk, hwcfg->csi_signalling_mode);
2823 
2824         if (!bus_cfg.nr_of_link_frequencies) {
2825                 dev_warn(dev, "no link frequencies defined\n");
2826                 goto out_err;
2827         }
2828 
2829         hwcfg->op_sys_clock = devm_kcalloc(
2830                 dev, bus_cfg.nr_of_link_frequencies + 1 /* guardian */,
2831                 sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
2832         if (!hwcfg->op_sys_clock)
2833                 goto out_err;
2834 
2835         for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
2836                 hwcfg->op_sys_clock[i] = bus_cfg.link_frequencies[i];
2837                 dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
2838         }
2839 
2840         v4l2_fwnode_endpoint_free(&bus_cfg);
2841         fwnode_handle_put(ep);
2842         return hwcfg;
2843 
2844 out_err:
2845         v4l2_fwnode_endpoint_free(&bus_cfg);
2846         fwnode_handle_put(ep);
2847         return NULL;
2848 }
2849 
2850 static int smiapp_probe(struct i2c_client *client)
2851 {
2852         struct smiapp_sensor *sensor;
2853         struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2854         unsigned int i;
2855         int rval;
2856 
2857         if (hwcfg == NULL)
2858                 return -ENODEV;
2859 
2860         sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
2861         if (sensor == NULL)
2862                 return -ENOMEM;
2863 
2864         sensor->hwcfg = hwcfg;
2865         mutex_init(&sensor->mutex);
2866         sensor->src = &sensor->ssds[sensor->ssds_used];
2867 
2868         v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
2869         sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2870 
2871         sensor->vana = devm_regulator_get(&client->dev, "vana");
2872         if (IS_ERR(sensor->vana)) {
2873                 dev_err(&client->dev, "could not get regulator for vana\n");
2874                 return PTR_ERR(sensor->vana);
2875         }
2876 
2877         sensor->ext_clk = devm_clk_get(&client->dev, NULL);
2878         if (PTR_ERR(sensor->ext_clk) == -ENOENT) {
2879                 dev_info(&client->dev, "no clock defined, continuing...\n");
2880                 sensor->ext_clk = NULL;
2881         } else if (IS_ERR(sensor->ext_clk)) {
2882                 dev_err(&client->dev, "could not get clock (%ld)\n",
2883                         PTR_ERR(sensor->ext_clk));
2884                 return -EPROBE_DEFER;
2885         }
2886 
2887         if (sensor->ext_clk) {
2888                 if (sensor->hwcfg->ext_clk) {
2889                         unsigned long rate;
2890 
2891                         rval = clk_set_rate(sensor->ext_clk,
2892                                             sensor->hwcfg->ext_clk);
2893                         if (rval < 0) {
2894                                 dev_err(&client->dev,
2895                                         "unable to set clock freq to %u\n",
2896                                         sensor->hwcfg->ext_clk);
2897                                 return rval;
2898                         }
2899 
2900                         rate = clk_get_rate(sensor->ext_clk);
2901                         if (rate != sensor->hwcfg->ext_clk) {
2902                                 dev_err(&client->dev,
2903                                         "can't set clock freq, asked for %u but got %lu\n",
2904                                         sensor->hwcfg->ext_clk, rate);
2905                                 return rval;
2906                         }
2907                 } else {
2908                         sensor->hwcfg->ext_clk = clk_get_rate(sensor->ext_clk);
2909                         dev_dbg(&client->dev, "obtained clock freq %u\n",
2910                                 sensor->hwcfg->ext_clk);
2911                 }
2912         } else if (sensor->hwcfg->ext_clk) {
2913                 dev_dbg(&client->dev, "assuming clock freq %u\n",
2914                         sensor->hwcfg->ext_clk);
2915         } else {
2916                 dev_err(&client->dev, "unable to obtain clock freq\n");
2917                 return -EINVAL;
2918         }
2919 
2920         sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
2921                                                     GPIOD_OUT_LOW);
2922         if (IS_ERR(sensor->xshutdown))
2923                 return PTR_ERR(sensor->xshutdown);
2924 
2925         rval = smiapp_power_on(&client->dev);
2926         if (rval < 0)
2927                 return rval;
2928 
2929         rval = smiapp_identify_module(sensor);
2930         if (rval) {
2931                 rval = -ENODEV;
2932                 goto out_power_off;
2933         }
2934 
2935         rval = smiapp_get_all_limits(sensor);
2936         if (rval) {
2937                 rval = -ENODEV;
2938                 goto out_power_off;
2939         }
2940 
2941         rval = smiapp_read_frame_fmt(sensor);
2942         if (rval) {
2943                 rval = -ENODEV;
2944                 goto out_power_off;
2945         }
2946 
2947         /*
2948          * Handle Sensor Module orientation on the board.
2949          *
2950          * The application of H-FLIP and V-FLIP on the sensor is modified by
2951          * the sensor orientation on the board.
2952          *
2953          * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
2954          * both H-FLIP and V-FLIP for normal operation which also implies
2955          * that a set/unset operation for user space HFLIP and VFLIP v4l2
2956          * controls will need to be internally inverted.
2957          *
2958          * Rotation also changes the bayer pattern.
2959          */
2960         if (sensor->hwcfg->module_board_orient ==
2961             SMIAPP_MODULE_BOARD_ORIENT_180)
2962                 sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
2963                                           SMIAPP_IMAGE_ORIENTATION_VFLIP;
2964 
2965         rval = smiapp_call_quirk(sensor, limits);
2966         if (rval) {
2967                 dev_err(&client->dev, "limits quirks failed\n");
2968                 goto out_power_off;
2969         }
2970 
2971         if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
2972                 u32 val;
2973 
2974                 rval = smiapp_read(sensor,
2975                                    SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
2976                 if (rval < 0) {
2977                         rval = -ENODEV;
2978                         goto out_power_off;
2979                 }
2980                 sensor->nbinning_subtypes = min_t(u8, val,
2981                                                   SMIAPP_BINNING_SUBTYPES);
2982 
2983                 for (i = 0; i < sensor->nbinning_subtypes; i++) {
2984                         rval = smiapp_read(
2985                                 sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
2986                         if (rval < 0) {
2987                                 rval = -ENODEV;
2988                                 goto out_power_off;
2989                         }
2990                         sensor->binning_subtypes[i] =
2991                                 *(struct smiapp_binning_subtype *)&val;
2992 
2993                         dev_dbg(&client->dev, "binning %xx%x\n",
2994                                 sensor->binning_subtypes[i].horizontal,
2995                                 sensor->binning_subtypes[i].vertical);
2996                 }
2997         }
2998         sensor->binning_horizontal = 1;
2999         sensor->binning_vertical = 1;
3000 
3001         if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
3002                 dev_err(&client->dev, "sysfs ident entry creation failed\n");
3003                 rval = -ENOENT;
3004                 goto out_power_off;
3005         }
3006         /* SMIA++ NVM initialization - it will be read from the sensor
3007          * when it is first requested by userspace.
3008          */
3009         if (sensor->minfo.smiapp_version && sensor->hwcfg->nvm_size) {
3010                 sensor->nvm = devm_kzalloc(&client->dev,
3011                                 sensor->hwcfg->nvm_size, GFP_KERNEL);
3012                 if (sensor->nvm == NULL) {
3013                         rval = -ENOMEM;
3014                         goto out_cleanup;
3015                 }
3016 
3017                 if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
3018                         dev_err(&client->dev, "sysfs nvm entry failed\n");
3019                         rval = -EBUSY;
3020                         goto out_cleanup;
3021                 }
3022         }
3023 
3024         /* We consider this as profile 0 sensor if any of these are zero. */
3025         if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
3026             !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
3027             !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
3028             !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
3029                 sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
3030         } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
3031                    != SMIAPP_SCALING_CAPABILITY_NONE) {
3032                 if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
3033                     == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
3034                         sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
3035                 else
3036                         sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
3037                 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3038                 sensor->ssds_used++;
3039         } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
3040                    == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
3041                 sensor->scaler = &sensor->ssds[sensor->ssds_used];
3042                 sensor->ssds_used++;
3043         }
3044         sensor->binner = &sensor->ssds[sensor->ssds_used];
3045         sensor->ssds_used++;
3046         sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
3047         sensor->ssds_used++;
3048 
3049         sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3050 
3051         /* prepare PLL configuration input values */
3052         sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
3053         sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
3054         sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
3055         sensor->pll.scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
3056         /* Profile 0 sensors have no separate OP clock branch. */
3057         if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
3058                 sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
3059 
3060         smiapp_create_subdev(sensor, sensor->scaler, " scaler", 2);
3061         smiapp_create_subdev(sensor, sensor->binner, " binner", 2);
3062         smiapp_create_subdev(sensor, sensor->pixel_array, " pixel_array", 1);
3063 
3064         dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
3065 
3066         sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3067 
3068         rval = smiapp_init_controls(sensor);
3069         if (rval < 0)
3070                 goto out_cleanup;
3071 
3072         rval = smiapp_call_quirk(sensor, init);
3073         if (rval)
3074                 goto out_cleanup;
3075 
3076         rval = smiapp_get_mbus_formats(sensor);
3077         if (rval) {
3078                 rval = -ENODEV;
3079                 goto out_cleanup;
3080         }
3081 
3082         rval = smiapp_init_late_controls(sensor);
3083         if (rval) {
3084                 rval = -ENODEV;
3085                 goto out_cleanup;
3086         }
3087 
3088         mutex_lock(&sensor->mutex);
3089         rval = smiapp_update_mode(sensor);
3090         mutex_unlock(&sensor->mutex);
3091         if (rval) {
3092                 dev_err(&client->dev, "update mode failed\n");
3093                 goto out_cleanup;
3094         }
3095 
3096         sensor->streaming = false;
3097         sensor->dev_init_done = true;
3098 
3099         rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
3100                                  sensor->src->pads);
3101         if (rval < 0)
3102                 goto out_media_entity_cleanup;
3103 
3104         pm_runtime_set_active(&client->dev);
3105         pm_runtime_get_noresume(&client->dev);
3106         pm_runtime_enable(&client->dev);
3107 
3108         rval = v4l2_async_register_subdev_sensor_common(&sensor->src->sd);
3109         if (rval < 0)
3110                 goto out_disable_runtime_pm;
3111 
3112         pm_runtime_set_autosuspend_delay(&client->dev, 1000);
3113         pm_runtime_use_autosuspend(&client->dev);
3114         pm_runtime_put_autosuspend(&client->dev);
3115 
3116         return 0;
3117 
3118 out_disable_runtime_pm:
3119         pm_runtime_disable(&client->dev);
3120 
3121 out_media_entity_cleanup:
3122         media_entity_cleanup(&sensor->src->sd.entity);
3123 
3124 out_cleanup:
3125         smiapp_cleanup(sensor);
3126 
3127 out_power_off:
3128         smiapp_power_off(&client->dev);
3129 
3130         return rval;
3131 }
3132 
3133 static int smiapp_remove(struct i2c_client *client)
3134 {
3135         struct v4l2_subdev *subdev = i2c_get_clientdata(client);
3136         struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
3137         unsigned int i;
3138 
3139         v4l2_async_unregister_subdev(subdev);
3140 
3141         pm_runtime_disable(&client->dev);
3142         if (!pm_runtime_status_suspended(&client->dev))
3143                 smiapp_power_off(&client->dev);
3144         pm_runtime_set_suspended(&client->dev);
3145 
3146         for (i = 0; i < sensor->ssds_used; i++) {
3147                 v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3148                 media_entity_cleanup(&sensor->ssds[i].sd.entity);
3149         }
3150         smiapp_cleanup(sensor);
3151 
3152         return 0;
3153 }
3154 
3155 static const struct of_device_id smiapp_of_table[] = {
3156         { .compatible = "nokia,smia" },
3157         { },
3158 };
3159 MODULE_DEVICE_TABLE(of, smiapp_of_table);
3160 
3161 static const struct i2c_device_id smiapp_id_table[] = {
3162         { SMIAPP_NAME, 0 },
3163         { },
3164 };
3165 MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
3166 
3167 static const struct dev_pm_ops smiapp_pm_ops = {
3168         SET_SYSTEM_SLEEP_PM_OPS(smiapp_suspend, smiapp_resume)
3169         SET_RUNTIME_PM_OPS(smiapp_power_off, smiapp_power_on, NULL)
3170 };
3171 
3172 static struct i2c_driver smiapp_i2c_driver = {
3173         .driver = {
3174                 .of_match_table = smiapp_of_table,
3175                 .name = SMIAPP_NAME,
3176                 .pm = &smiapp_pm_ops,
3177         },
3178         .probe_new = smiapp_probe,
3179         .remove = smiapp_remove,
3180         .id_table = smiapp_id_table,
3181 };
3182 
3183 module_i2c_driver(smiapp_i2c_driver);
3184 
3185 MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
3186 MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
3187 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */