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