treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / atmel / atmel-isc-base.c
blobd7669a03e98ea14c2d7d4af0c86531fb4334c8c2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Microchip Image Sensor Controller (ISC) common driver base
5 * Copyright (C) 2016-2019 Microchip Technology, Inc.
7 * Author: Songjun Wu
8 * Author: Eugen Hristev <eugen.hristev@microchip.com>
12 #include <linux/clk.h>
13 #include <linux/clkdev.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/math64.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/of_graph.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/videodev2.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-image-sizes.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-fwnode.h>
32 #include <media/v4l2-subdev.h>
33 #include <media/videobuf2-dma-contig.h>
35 #include "atmel-isc-regs.h"
36 #include "atmel-isc.h"
38 static unsigned int debug;
39 module_param(debug, int, 0644);
40 MODULE_PARM_DESC(debug, "debug level (0-2)");
42 static unsigned int sensor_preferred = 1;
43 module_param(sensor_preferred, uint, 0644);
44 MODULE_PARM_DESC(sensor_preferred,
45 "Sensor is preferred to output the specified format (1-on 0-off), default 1");
47 /* This is a list of the formats that the ISC can *output* */
48 const struct isc_format controller_formats[] = {
50 .fourcc = V4L2_PIX_FMT_ARGB444,
53 .fourcc = V4L2_PIX_FMT_ARGB555,
56 .fourcc = V4L2_PIX_FMT_RGB565,
59 .fourcc = V4L2_PIX_FMT_ABGR32,
62 .fourcc = V4L2_PIX_FMT_XBGR32,
65 .fourcc = V4L2_PIX_FMT_YUV420,
68 .fourcc = V4L2_PIX_FMT_YUYV,
71 .fourcc = V4L2_PIX_FMT_YUV422P,
74 .fourcc = V4L2_PIX_FMT_GREY,
77 .fourcc = V4L2_PIX_FMT_Y10,
81 /* This is a list of formats that the ISC can receive as *input* */
82 struct isc_format formats_list[] = {
84 .fourcc = V4L2_PIX_FMT_SBGGR8,
85 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
86 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
87 .cfa_baycfg = ISC_BAY_CFG_BGBG,
90 .fourcc = V4L2_PIX_FMT_SGBRG8,
91 .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
92 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
93 .cfa_baycfg = ISC_BAY_CFG_GBGB,
96 .fourcc = V4L2_PIX_FMT_SGRBG8,
97 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
98 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
99 .cfa_baycfg = ISC_BAY_CFG_GRGR,
102 .fourcc = V4L2_PIX_FMT_SRGGB8,
103 .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
104 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
105 .cfa_baycfg = ISC_BAY_CFG_RGRG,
108 .fourcc = V4L2_PIX_FMT_SBGGR10,
109 .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
110 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
111 .cfa_baycfg = ISC_BAY_CFG_RGRG,
114 .fourcc = V4L2_PIX_FMT_SGBRG10,
115 .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
116 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
117 .cfa_baycfg = ISC_BAY_CFG_GBGB,
120 .fourcc = V4L2_PIX_FMT_SGRBG10,
121 .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
122 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
123 .cfa_baycfg = ISC_BAY_CFG_GRGR,
126 .fourcc = V4L2_PIX_FMT_SRGGB10,
127 .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
128 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
129 .cfa_baycfg = ISC_BAY_CFG_RGRG,
132 .fourcc = V4L2_PIX_FMT_SBGGR12,
133 .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
134 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
135 .cfa_baycfg = ISC_BAY_CFG_BGBG,
138 .fourcc = V4L2_PIX_FMT_SGBRG12,
139 .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
140 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
141 .cfa_baycfg = ISC_BAY_CFG_GBGB,
144 .fourcc = V4L2_PIX_FMT_SGRBG12,
145 .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
146 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
147 .cfa_baycfg = ISC_BAY_CFG_GRGR,
150 .fourcc = V4L2_PIX_FMT_SRGGB12,
151 .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
152 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
153 .cfa_baycfg = ISC_BAY_CFG_RGRG,
156 .fourcc = V4L2_PIX_FMT_GREY,
157 .mbus_code = MEDIA_BUS_FMT_Y8_1X8,
158 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
161 .fourcc = V4L2_PIX_FMT_YUYV,
162 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
163 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
166 .fourcc = V4L2_PIX_FMT_RGB565,
167 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
168 .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
171 .fourcc = V4L2_PIX_FMT_Y10,
172 .mbus_code = MEDIA_BUS_FMT_Y10_1X10,
173 .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
178 /* Gamma table with gamma 1/2.2 */
179 const u32 isc_gamma_table[GAMMA_MAX + 1][GAMMA_ENTRIES] = {
180 /* 0 --> gamma 1/1.8 */
181 { 0x65, 0x66002F, 0x950025, 0xBB0020, 0xDB001D, 0xF8001A,
182 0x1130018, 0x12B0017, 0x1420016, 0x1580014, 0x16D0013, 0x1810012,
183 0x1940012, 0x1A60012, 0x1B80011, 0x1C90010, 0x1DA0010, 0x1EA000F,
184 0x1FA000F, 0x209000F, 0x218000F, 0x227000E, 0x235000E, 0x243000E,
185 0x251000E, 0x25F000D, 0x26C000D, 0x279000D, 0x286000D, 0x293000C,
186 0x2A0000C, 0x2AC000C, 0x2B8000C, 0x2C4000C, 0x2D0000B, 0x2DC000B,
187 0x2E7000B, 0x2F3000B, 0x2FE000B, 0x309000B, 0x314000B, 0x31F000A,
188 0x32A000A, 0x334000B, 0x33F000A, 0x349000A, 0x354000A, 0x35E000A,
189 0x368000A, 0x372000A, 0x37C000A, 0x386000A, 0x3900009, 0x399000A,
190 0x3A30009, 0x3AD0009, 0x3B60009, 0x3BF000A, 0x3C90009, 0x3D20009,
191 0x3DB0009, 0x3E40009, 0x3ED0009, 0x3F60009 },
193 /* 1 --> gamma 1/2 */
194 { 0x7F, 0x800034, 0xB50028, 0xDE0021, 0x100001E, 0x11E001B,
195 0x1390019, 0x1520017, 0x16A0015, 0x1800014, 0x1940014, 0x1A80013,
196 0x1BB0012, 0x1CD0011, 0x1DF0010, 0x1EF0010, 0x200000F, 0x20F000F,
197 0x21F000E, 0x22D000F, 0x23C000E, 0x24A000E, 0x258000D, 0x265000D,
198 0x273000C, 0x27F000D, 0x28C000C, 0x299000C, 0x2A5000C, 0x2B1000B,
199 0x2BC000C, 0x2C8000B, 0x2D3000C, 0x2DF000B, 0x2EA000A, 0x2F5000A,
200 0x2FF000B, 0x30A000A, 0x314000B, 0x31F000A, 0x329000A, 0x333000A,
201 0x33D0009, 0x3470009, 0x350000A, 0x35A0009, 0x363000A, 0x36D0009,
202 0x3760009, 0x37F0009, 0x3880009, 0x3910009, 0x39A0009, 0x3A30009,
203 0x3AC0008, 0x3B40009, 0x3BD0008, 0x3C60008, 0x3CE0008, 0x3D60009,
204 0x3DF0008, 0x3E70008, 0x3EF0008, 0x3F70008 },
206 /* 2 --> gamma 1/2.2 */
207 { 0x99, 0x9B0038, 0xD4002A, 0xFF0023, 0x122001F, 0x141001B,
208 0x15D0019, 0x1760017, 0x18E0015, 0x1A30015, 0x1B80013, 0x1CC0012,
209 0x1DE0011, 0x1F00010, 0x2010010, 0x2110010, 0x221000F, 0x230000F,
210 0x23F000E, 0x24D000E, 0x25B000D, 0x269000C, 0x276000C, 0x283000C,
211 0x28F000C, 0x29B000C, 0x2A7000C, 0x2B3000B, 0x2BF000B, 0x2CA000B,
212 0x2D5000B, 0x2E0000A, 0x2EB000A, 0x2F5000A, 0x2FF000A, 0x30A000A,
213 0x3140009, 0x31E0009, 0x327000A, 0x3310009, 0x33A0009, 0x3440009,
214 0x34D0009, 0x3560009, 0x35F0009, 0x3680008, 0x3710008, 0x3790009,
215 0x3820008, 0x38A0008, 0x3930008, 0x39B0008, 0x3A30008, 0x3AB0008,
216 0x3B30008, 0x3BB0008, 0x3C30008, 0x3CB0007, 0x3D20008, 0x3DA0007,
217 0x3E20007, 0x3E90007, 0x3F00008, 0x3F80007 },
220 #define ISC_IS_FORMAT_RAW(mbus_code) \
221 (((mbus_code) & 0xf000) == 0x3000)
223 #define ISC_IS_FORMAT_GREY(mbus_code) \
224 (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
225 (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
227 static inline void isc_update_awb_ctrls(struct isc_device *isc)
229 struct isc_ctrls *ctrls = &isc->ctrls;
231 regmap_write(isc->regmap, ISC_WB_O_RGR,
232 (ISC_WB_O_ZERO_VAL - (ctrls->offset[ISC_HIS_CFG_MODE_R])) |
233 ((ISC_WB_O_ZERO_VAL - ctrls->offset[ISC_HIS_CFG_MODE_GR]) << 16));
234 regmap_write(isc->regmap, ISC_WB_O_BGB,
235 (ISC_WB_O_ZERO_VAL - (ctrls->offset[ISC_HIS_CFG_MODE_B])) |
236 ((ISC_WB_O_ZERO_VAL - ctrls->offset[ISC_HIS_CFG_MODE_GB]) << 16));
237 regmap_write(isc->regmap, ISC_WB_G_RGR,
238 ctrls->gain[ISC_HIS_CFG_MODE_R] |
239 (ctrls->gain[ISC_HIS_CFG_MODE_GR] << 16));
240 regmap_write(isc->regmap, ISC_WB_G_BGB,
241 ctrls->gain[ISC_HIS_CFG_MODE_B] |
242 (ctrls->gain[ISC_HIS_CFG_MODE_GB] << 16));
245 static inline void isc_reset_awb_ctrls(struct isc_device *isc)
247 unsigned int c;
249 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
250 /* gains have a fixed point at 9 decimals */
251 isc->ctrls.gain[c] = 1 << 9;
252 /* offsets are in 2's complements, the value
253 * will be substracted from ISC_WB_O_ZERO_VAL to obtain
254 * 2's complement of a value between 0 and
255 * ISC_WB_O_ZERO_VAL >> 1
257 isc->ctrls.offset[c] = ISC_WB_O_ZERO_VAL;
261 static int isc_wait_clk_stable(struct clk_hw *hw)
263 struct isc_clk *isc_clk = to_isc_clk(hw);
264 struct regmap *regmap = isc_clk->regmap;
265 unsigned long timeout = jiffies + usecs_to_jiffies(1000);
266 unsigned int status;
268 while (time_before(jiffies, timeout)) {
269 regmap_read(regmap, ISC_CLKSR, &status);
270 if (!(status & ISC_CLKSR_SIP))
271 return 0;
273 usleep_range(10, 250);
276 return -ETIMEDOUT;
279 static int isc_clk_prepare(struct clk_hw *hw)
281 struct isc_clk *isc_clk = to_isc_clk(hw);
283 if (isc_clk->id == ISC_ISPCK)
284 pm_runtime_get_sync(isc_clk->dev);
286 return isc_wait_clk_stable(hw);
289 static void isc_clk_unprepare(struct clk_hw *hw)
291 struct isc_clk *isc_clk = to_isc_clk(hw);
293 isc_wait_clk_stable(hw);
295 if (isc_clk->id == ISC_ISPCK)
296 pm_runtime_put_sync(isc_clk->dev);
299 static int isc_clk_enable(struct clk_hw *hw)
301 struct isc_clk *isc_clk = to_isc_clk(hw);
302 u32 id = isc_clk->id;
303 struct regmap *regmap = isc_clk->regmap;
304 unsigned long flags;
305 unsigned int status;
307 dev_dbg(isc_clk->dev, "ISC CLK: %s, div = %d, parent id = %d\n",
308 __func__, isc_clk->div, isc_clk->parent_id);
310 spin_lock_irqsave(&isc_clk->lock, flags);
311 regmap_update_bits(regmap, ISC_CLKCFG,
312 ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
313 (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
314 (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
316 regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
317 spin_unlock_irqrestore(&isc_clk->lock, flags);
319 regmap_read(regmap, ISC_CLKSR, &status);
320 if (status & ISC_CLK(id))
321 return 0;
322 else
323 return -EINVAL;
326 static void isc_clk_disable(struct clk_hw *hw)
328 struct isc_clk *isc_clk = to_isc_clk(hw);
329 u32 id = isc_clk->id;
330 unsigned long flags;
332 spin_lock_irqsave(&isc_clk->lock, flags);
333 regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
334 spin_unlock_irqrestore(&isc_clk->lock, flags);
337 static int isc_clk_is_enabled(struct clk_hw *hw)
339 struct isc_clk *isc_clk = to_isc_clk(hw);
340 u32 status;
342 if (isc_clk->id == ISC_ISPCK)
343 pm_runtime_get_sync(isc_clk->dev);
345 regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
347 if (isc_clk->id == ISC_ISPCK)
348 pm_runtime_put_sync(isc_clk->dev);
350 return status & ISC_CLK(isc_clk->id) ? 1 : 0;
353 static unsigned long
354 isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
356 struct isc_clk *isc_clk = to_isc_clk(hw);
358 return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
361 static int isc_clk_determine_rate(struct clk_hw *hw,
362 struct clk_rate_request *req)
364 struct isc_clk *isc_clk = to_isc_clk(hw);
365 long best_rate = -EINVAL;
366 int best_diff = -1;
367 unsigned int i, div;
369 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
370 struct clk_hw *parent;
371 unsigned long parent_rate;
373 parent = clk_hw_get_parent_by_index(hw, i);
374 if (!parent)
375 continue;
377 parent_rate = clk_hw_get_rate(parent);
378 if (!parent_rate)
379 continue;
381 for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
382 unsigned long rate;
383 int diff;
385 rate = DIV_ROUND_CLOSEST(parent_rate, div);
386 diff = abs(req->rate - rate);
388 if (best_diff < 0 || best_diff > diff) {
389 best_rate = rate;
390 best_diff = diff;
391 req->best_parent_rate = parent_rate;
392 req->best_parent_hw = parent;
395 if (!best_diff || rate < req->rate)
396 break;
399 if (!best_diff)
400 break;
403 dev_dbg(isc_clk->dev,
404 "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
405 __func__, best_rate,
406 __clk_get_name((req->best_parent_hw)->clk),
407 req->best_parent_rate);
409 if (best_rate < 0)
410 return best_rate;
412 req->rate = best_rate;
414 return 0;
417 static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
419 struct isc_clk *isc_clk = to_isc_clk(hw);
421 if (index >= clk_hw_get_num_parents(hw))
422 return -EINVAL;
424 isc_clk->parent_id = index;
426 return 0;
429 static u8 isc_clk_get_parent(struct clk_hw *hw)
431 struct isc_clk *isc_clk = to_isc_clk(hw);
433 return isc_clk->parent_id;
436 static int isc_clk_set_rate(struct clk_hw *hw,
437 unsigned long rate,
438 unsigned long parent_rate)
440 struct isc_clk *isc_clk = to_isc_clk(hw);
441 u32 div;
443 if (!rate)
444 return -EINVAL;
446 div = DIV_ROUND_CLOSEST(parent_rate, rate);
447 if (div > (ISC_CLK_MAX_DIV + 1) || !div)
448 return -EINVAL;
450 isc_clk->div = div - 1;
452 return 0;
455 static const struct clk_ops isc_clk_ops = {
456 .prepare = isc_clk_prepare,
457 .unprepare = isc_clk_unprepare,
458 .enable = isc_clk_enable,
459 .disable = isc_clk_disable,
460 .is_enabled = isc_clk_is_enabled,
461 .recalc_rate = isc_clk_recalc_rate,
462 .determine_rate = isc_clk_determine_rate,
463 .set_parent = isc_clk_set_parent,
464 .get_parent = isc_clk_get_parent,
465 .set_rate = isc_clk_set_rate,
468 static int isc_clk_register(struct isc_device *isc, unsigned int id)
470 struct regmap *regmap = isc->regmap;
471 struct device_node *np = isc->dev->of_node;
472 struct isc_clk *isc_clk;
473 struct clk_init_data init;
474 const char *clk_name = np->name;
475 const char *parent_names[3];
476 int num_parents;
478 num_parents = of_clk_get_parent_count(np);
479 if (num_parents < 1 || num_parents > 3)
480 return -EINVAL;
482 if (num_parents > 2 && id == ISC_ISPCK)
483 num_parents = 2;
485 of_clk_parent_fill(np, parent_names, num_parents);
487 if (id == ISC_MCK)
488 of_property_read_string(np, "clock-output-names", &clk_name);
489 else
490 clk_name = "isc-ispck";
492 init.parent_names = parent_names;
493 init.num_parents = num_parents;
494 init.name = clk_name;
495 init.ops = &isc_clk_ops;
496 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
498 isc_clk = &isc->isc_clks[id];
499 isc_clk->hw.init = &init;
500 isc_clk->regmap = regmap;
501 isc_clk->id = id;
502 isc_clk->dev = isc->dev;
503 spin_lock_init(&isc_clk->lock);
505 isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
506 if (IS_ERR(isc_clk->clk)) {
507 dev_err(isc->dev, "%s: clock register fail\n", clk_name);
508 return PTR_ERR(isc_clk->clk);
509 } else if (id == ISC_MCK)
510 of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
512 return 0;
515 int isc_clk_init(struct isc_device *isc)
517 unsigned int i;
518 int ret;
520 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
521 isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
523 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
524 ret = isc_clk_register(isc, i);
525 if (ret)
526 return ret;
529 return 0;
532 void isc_clk_cleanup(struct isc_device *isc)
534 unsigned int i;
536 of_clk_del_provider(isc->dev->of_node);
538 for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
539 struct isc_clk *isc_clk = &isc->isc_clks[i];
541 if (!IS_ERR(isc_clk->clk))
542 clk_unregister(isc_clk->clk);
546 static int isc_queue_setup(struct vb2_queue *vq,
547 unsigned int *nbuffers, unsigned int *nplanes,
548 unsigned int sizes[], struct device *alloc_devs[])
550 struct isc_device *isc = vb2_get_drv_priv(vq);
551 unsigned int size = isc->fmt.fmt.pix.sizeimage;
553 if (*nplanes)
554 return sizes[0] < size ? -EINVAL : 0;
556 *nplanes = 1;
557 sizes[0] = size;
559 return 0;
562 static int isc_buffer_prepare(struct vb2_buffer *vb)
564 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
565 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
566 unsigned long size = isc->fmt.fmt.pix.sizeimage;
568 if (vb2_plane_size(vb, 0) < size) {
569 v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
570 vb2_plane_size(vb, 0), size);
571 return -EINVAL;
574 vb2_set_plane_payload(vb, 0, size);
576 vbuf->field = isc->fmt.fmt.pix.field;
578 return 0;
581 static void isc_start_dma(struct isc_device *isc)
583 struct regmap *regmap = isc->regmap;
584 u32 sizeimage = isc->fmt.fmt.pix.sizeimage;
585 u32 dctrl_dview;
586 dma_addr_t addr0;
587 u32 h, w;
589 h = isc->fmt.fmt.pix.height;
590 w = isc->fmt.fmt.pix.width;
593 * In case the sensor is not RAW, it will output a pixel (12-16 bits)
594 * with two samples on the ISC Data bus (which is 8-12)
595 * ISC will count each sample, so, we need to multiply these values
596 * by two, to get the real number of samples for the required pixels.
598 if (!ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code)) {
599 h <<= 1;
600 w <<= 1;
604 * We limit the column/row count that the ISC will output according
605 * to the configured resolution that we want.
606 * This will avoid the situation where the sensor is misconfigured,
607 * sending more data, and the ISC will just take it and DMA to memory,
608 * causing corruption.
610 regmap_write(regmap, ISC_PFE_CFG1,
611 (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK) |
612 (ISC_PFE_CFG1_COLMAX(w - 1) & ISC_PFE_CFG1_COLMAX_MASK));
614 regmap_write(regmap, ISC_PFE_CFG2,
615 (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK) |
616 (ISC_PFE_CFG2_ROWMAX(h - 1) & ISC_PFE_CFG2_ROWMAX_MASK));
618 regmap_update_bits(regmap, ISC_PFE_CFG0,
619 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN,
620 ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN);
622 addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
623 regmap_write(regmap, ISC_DAD0, addr0);
625 switch (isc->config.fourcc) {
626 case V4L2_PIX_FMT_YUV420:
627 regmap_write(regmap, ISC_DAD1, addr0 + (sizeimage * 2) / 3);
628 regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 5) / 6);
629 break;
630 case V4L2_PIX_FMT_YUV422P:
631 regmap_write(regmap, ISC_DAD1, addr0 + sizeimage / 2);
632 regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 3) / 4);
633 break;
634 default:
635 break;
638 dctrl_dview = isc->config.dctrl_dview;
640 regmap_write(regmap, ISC_DCTRL, dctrl_dview | ISC_DCTRL_IE_IS);
641 spin_lock(&isc->awb_lock);
642 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
643 spin_unlock(&isc->awb_lock);
646 static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
648 struct regmap *regmap = isc->regmap;
649 struct isc_ctrls *ctrls = &isc->ctrls;
650 u32 val, bay_cfg;
651 const u32 *gamma;
652 unsigned int i;
654 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
655 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
656 val = pipeline & BIT(i) ? 1 : 0;
657 regmap_field_write(isc->pipeline[i], val);
660 if (!pipeline)
661 return;
663 bay_cfg = isc->config.sd_format->cfa_baycfg;
665 if (ctrls->awb == ISC_WB_NONE)
666 isc_reset_awb_ctrls(isc);
668 regmap_write(regmap, ISC_WB_CFG, bay_cfg);
669 isc_update_awb_ctrls(isc);
671 regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
673 gamma = &isc_gamma_table[ctrls->gamma_index][0];
674 regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
675 regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
676 regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
678 /* Convert RGB to YUV */
679 regmap_write(regmap, ISC_CSC_YR_YG, 0x42 | (0x81 << 16));
680 regmap_write(regmap, ISC_CSC_YB_OY, 0x19 | (0x10 << 16));
681 regmap_write(regmap, ISC_CSC_CBR_CBG, 0xFDA | (0xFB6 << 16));
682 regmap_write(regmap, ISC_CSC_CBB_OCB, 0x70 | (0x80 << 16));
683 regmap_write(regmap, ISC_CSC_CRR_CRG, 0x70 | (0xFA2 << 16));
684 regmap_write(regmap, ISC_CSC_CRB_OCR, 0xFEE | (0x80 << 16));
686 regmap_write(regmap, ISC_CBC_BRIGHT, ctrls->brightness);
687 regmap_write(regmap, ISC_CBC_CONTRAST, ctrls->contrast);
690 static int isc_update_profile(struct isc_device *isc)
692 struct regmap *regmap = isc->regmap;
693 u32 sr;
694 int counter = 100;
696 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
698 regmap_read(regmap, ISC_CTRLSR, &sr);
699 while ((sr & ISC_CTRL_UPPRO) && counter--) {
700 usleep_range(1000, 2000);
701 regmap_read(regmap, ISC_CTRLSR, &sr);
704 if (counter < 0) {
705 v4l2_warn(&isc->v4l2_dev, "Time out to update profile\n");
706 return -ETIMEDOUT;
709 return 0;
712 static void isc_set_histogram(struct isc_device *isc, bool enable)
714 struct regmap *regmap = isc->regmap;
715 struct isc_ctrls *ctrls = &isc->ctrls;
717 if (enable) {
718 regmap_write(regmap, ISC_HIS_CFG,
719 ISC_HIS_CFG_MODE_GR |
720 (isc->config.sd_format->cfa_baycfg
721 << ISC_HIS_CFG_BAYSEL_SHIFT) |
722 ISC_HIS_CFG_RAR);
723 regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_EN);
724 regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
725 ctrls->hist_id = ISC_HIS_CFG_MODE_GR;
726 isc_update_profile(isc);
727 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
729 ctrls->hist_stat = HIST_ENABLED;
730 } else {
731 regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
732 regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_DIS);
734 ctrls->hist_stat = HIST_DISABLED;
738 static int isc_configure(struct isc_device *isc)
740 struct regmap *regmap = isc->regmap;
741 u32 pfe_cfg0, rlp_mode, dcfg, mask, pipeline;
742 struct isc_subdev_entity *subdev = isc->current_subdev;
744 pfe_cfg0 = isc->config.sd_format->pfe_cfg0_bps;
745 rlp_mode = isc->config.rlp_cfg_mode;
746 pipeline = isc->config.bits_pipeline;
748 dcfg = isc->config.dcfg_imode |
749 ISC_DCFG_YMBSIZE_BEATS8 | ISC_DCFG_CMBSIZE_BEATS8;
751 pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
752 mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
753 ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
754 ISC_PFE_CFG0_MODE_MASK | ISC_PFE_CFG0_CCIR_CRC |
755 ISC_PFE_CFG0_CCIR656;
757 regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
759 regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
760 rlp_mode);
762 regmap_write(regmap, ISC_DCFG, dcfg);
764 /* Set the pipeline */
765 isc_set_pipeline(isc, pipeline);
768 * The current implemented histogram is available for RAW R, B, GB, GR
769 * channels. We need to check if sensor is outputting RAW BAYER
771 if (isc->ctrls.awb &&
772 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
773 isc_set_histogram(isc, true);
774 else
775 isc_set_histogram(isc, false);
777 /* Update profile */
778 return isc_update_profile(isc);
781 static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
783 struct isc_device *isc = vb2_get_drv_priv(vq);
784 struct regmap *regmap = isc->regmap;
785 struct isc_buffer *buf;
786 unsigned long flags;
787 int ret;
789 /* Enable stream on the sub device */
790 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
791 if (ret && ret != -ENOIOCTLCMD) {
792 v4l2_err(&isc->v4l2_dev, "stream on failed in subdev %d\n",
793 ret);
794 goto err_start_stream;
797 pm_runtime_get_sync(isc->dev);
799 ret = isc_configure(isc);
800 if (unlikely(ret))
801 goto err_configure;
803 /* Enable DMA interrupt */
804 regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
806 spin_lock_irqsave(&isc->dma_queue_lock, flags);
808 isc->sequence = 0;
809 isc->stop = false;
810 reinit_completion(&isc->comp);
812 isc->cur_frm = list_first_entry(&isc->dma_queue,
813 struct isc_buffer, list);
814 list_del(&isc->cur_frm->list);
816 isc_start_dma(isc);
818 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
820 /* if we streaming from RAW, we can do one-shot white balance adj */
821 if (ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
822 v4l2_ctrl_activate(isc->do_wb_ctrl, true);
824 return 0;
826 err_configure:
827 pm_runtime_put_sync(isc->dev);
829 v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
831 err_start_stream:
832 spin_lock_irqsave(&isc->dma_queue_lock, flags);
833 list_for_each_entry(buf, &isc->dma_queue, list)
834 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
835 INIT_LIST_HEAD(&isc->dma_queue);
836 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
838 return ret;
841 static void isc_stop_streaming(struct vb2_queue *vq)
843 struct isc_device *isc = vb2_get_drv_priv(vq);
844 unsigned long flags;
845 struct isc_buffer *buf;
846 int ret;
848 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
850 isc->stop = true;
852 /* Wait until the end of the current frame */
853 if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
854 v4l2_err(&isc->v4l2_dev,
855 "Timeout waiting for end of the capture\n");
857 /* Disable DMA interrupt */
858 regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
860 pm_runtime_put_sync(isc->dev);
862 /* Disable stream on the sub device */
863 ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
864 if (ret && ret != -ENOIOCTLCMD)
865 v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
867 /* Release all active buffers */
868 spin_lock_irqsave(&isc->dma_queue_lock, flags);
869 if (unlikely(isc->cur_frm)) {
870 vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
871 VB2_BUF_STATE_ERROR);
872 isc->cur_frm = NULL;
874 list_for_each_entry(buf, &isc->dma_queue, list)
875 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
876 INIT_LIST_HEAD(&isc->dma_queue);
877 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
880 static void isc_buffer_queue(struct vb2_buffer *vb)
882 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
883 struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
884 struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
885 unsigned long flags;
887 spin_lock_irqsave(&isc->dma_queue_lock, flags);
888 if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
889 vb2_is_streaming(vb->vb2_queue)) {
890 isc->cur_frm = buf;
891 isc_start_dma(isc);
892 } else
893 list_add_tail(&buf->list, &isc->dma_queue);
894 spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
897 static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
898 unsigned int fourcc)
900 unsigned int num_formats = isc->num_user_formats;
901 struct isc_format *fmt;
902 unsigned int i;
904 for (i = 0; i < num_formats; i++) {
905 fmt = isc->user_formats[i];
906 if (fmt->fourcc == fourcc)
907 return fmt;
910 return NULL;
913 static const struct vb2_ops isc_vb2_ops = {
914 .queue_setup = isc_queue_setup,
915 .wait_prepare = vb2_ops_wait_prepare,
916 .wait_finish = vb2_ops_wait_finish,
917 .buf_prepare = isc_buffer_prepare,
918 .start_streaming = isc_start_streaming,
919 .stop_streaming = isc_stop_streaming,
920 .buf_queue = isc_buffer_queue,
923 static int isc_querycap(struct file *file, void *priv,
924 struct v4l2_capability *cap)
926 struct isc_device *isc = video_drvdata(file);
928 strscpy(cap->driver, ATMEL_ISC_NAME, sizeof(cap->driver));
929 strscpy(cap->card, "Atmel Image Sensor Controller", sizeof(cap->card));
930 snprintf(cap->bus_info, sizeof(cap->bus_info),
931 "platform:%s", isc->v4l2_dev.name);
933 return 0;
936 static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
937 struct v4l2_fmtdesc *f)
939 u32 index = f->index;
940 u32 i, supported_index;
942 if (index < ARRAY_SIZE(controller_formats)) {
943 f->pixelformat = controller_formats[index].fourcc;
944 return 0;
947 index -= ARRAY_SIZE(controller_formats);
949 i = 0;
950 supported_index = 0;
952 for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
953 if (!ISC_IS_FORMAT_RAW(formats_list[i].mbus_code) ||
954 !formats_list[i].sd_support)
955 continue;
956 if (supported_index == index) {
957 f->pixelformat = formats_list[i].fourcc;
958 return 0;
960 supported_index++;
963 return -EINVAL;
966 static int isc_g_fmt_vid_cap(struct file *file, void *priv,
967 struct v4l2_format *fmt)
969 struct isc_device *isc = video_drvdata(file);
971 *fmt = isc->fmt;
973 return 0;
977 * Checks the current configured format, if ISC can output it,
978 * considering which type of format the ISC receives from the sensor
980 static int isc_try_validate_formats(struct isc_device *isc)
982 int ret;
983 bool bayer = false, yuv = false, rgb = false, grey = false;
985 /* all formats supported by the RLP module are OK */
986 switch (isc->try_config.fourcc) {
987 case V4L2_PIX_FMT_SBGGR8:
988 case V4L2_PIX_FMT_SGBRG8:
989 case V4L2_PIX_FMT_SGRBG8:
990 case V4L2_PIX_FMT_SRGGB8:
991 case V4L2_PIX_FMT_SBGGR10:
992 case V4L2_PIX_FMT_SGBRG10:
993 case V4L2_PIX_FMT_SGRBG10:
994 case V4L2_PIX_FMT_SRGGB10:
995 case V4L2_PIX_FMT_SBGGR12:
996 case V4L2_PIX_FMT_SGBRG12:
997 case V4L2_PIX_FMT_SGRBG12:
998 case V4L2_PIX_FMT_SRGGB12:
999 ret = 0;
1000 bayer = true;
1001 break;
1003 case V4L2_PIX_FMT_YUV420:
1004 case V4L2_PIX_FMT_YUV422P:
1005 case V4L2_PIX_FMT_YUYV:
1006 ret = 0;
1007 yuv = true;
1008 break;
1010 case V4L2_PIX_FMT_RGB565:
1011 case V4L2_PIX_FMT_ABGR32:
1012 case V4L2_PIX_FMT_XBGR32:
1013 case V4L2_PIX_FMT_ARGB444:
1014 case V4L2_PIX_FMT_ARGB555:
1015 ret = 0;
1016 rgb = true;
1017 break;
1018 case V4L2_PIX_FMT_GREY:
1019 case V4L2_PIX_FMT_Y10:
1020 ret = 0;
1021 grey = true;
1022 break;
1023 default:
1024 /* any other different formats are not supported */
1025 ret = -EINVAL;
1027 v4l2_dbg(1, debug, &isc->v4l2_dev,
1028 "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
1029 rgb, yuv, grey, bayer);
1031 /* we cannot output RAW if we do not receive RAW */
1032 if ((bayer) && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
1033 return -EINVAL;
1035 /* we cannot output GREY if we do not receive RAW/GREY */
1036 if (grey && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code) &&
1037 !ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code))
1038 return -EINVAL;
1040 return ret;
1044 * Configures the RLP and DMA modules, depending on the output format
1045 * configured for the ISC.
1046 * If direct_dump == true, just dump raw data 8/16 bits depending on format.
1048 static int isc_try_configure_rlp_dma(struct isc_device *isc, bool direct_dump)
1050 switch (isc->try_config.fourcc) {
1051 case V4L2_PIX_FMT_SBGGR8:
1052 case V4L2_PIX_FMT_SGBRG8:
1053 case V4L2_PIX_FMT_SGRBG8:
1054 case V4L2_PIX_FMT_SRGGB8:
1055 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1056 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1057 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1058 isc->try_config.bpp = 8;
1059 break;
1060 case V4L2_PIX_FMT_SBGGR10:
1061 case V4L2_PIX_FMT_SGBRG10:
1062 case V4L2_PIX_FMT_SGRBG10:
1063 case V4L2_PIX_FMT_SRGGB10:
1064 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10;
1065 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1066 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1067 isc->try_config.bpp = 16;
1068 break;
1069 case V4L2_PIX_FMT_SBGGR12:
1070 case V4L2_PIX_FMT_SGBRG12:
1071 case V4L2_PIX_FMT_SGRBG12:
1072 case V4L2_PIX_FMT_SRGGB12:
1073 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12;
1074 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1075 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1076 isc->try_config.bpp = 16;
1077 break;
1078 case V4L2_PIX_FMT_RGB565:
1079 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565;
1080 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1081 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1082 isc->try_config.bpp = 16;
1083 break;
1084 case V4L2_PIX_FMT_ARGB444:
1085 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444;
1086 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1087 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1088 isc->try_config.bpp = 16;
1089 break;
1090 case V4L2_PIX_FMT_ARGB555:
1091 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555;
1092 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1093 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1094 isc->try_config.bpp = 16;
1095 break;
1096 case V4L2_PIX_FMT_ABGR32:
1097 case V4L2_PIX_FMT_XBGR32:
1098 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32;
1099 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1100 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1101 isc->try_config.bpp = 32;
1102 break;
1103 case V4L2_PIX_FMT_YUV420:
1104 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1105 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC420P;
1106 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1107 isc->try_config.bpp = 12;
1108 break;
1109 case V4L2_PIX_FMT_YUV422P:
1110 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1111 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC422P;
1112 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1113 isc->try_config.bpp = 16;
1114 break;
1115 case V4L2_PIX_FMT_YUYV:
1116 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1117 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1118 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1119 isc->try_config.bpp = 16;
1120 break;
1121 case V4L2_PIX_FMT_GREY:
1122 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8;
1123 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1124 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1125 isc->try_config.bpp = 8;
1126 break;
1127 case V4L2_PIX_FMT_Y10:
1128 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY10;
1129 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1130 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1131 isc->try_config.bpp = 16;
1132 break;
1133 default:
1134 return -EINVAL;
1137 if (direct_dump) {
1138 isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1139 isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1140 isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1141 return 0;
1144 return 0;
1148 * Configuring pipeline modules, depending on which format the ISC outputs
1149 * and considering which format it has as input from the sensor.
1151 static int isc_try_configure_pipeline(struct isc_device *isc)
1153 switch (isc->try_config.fourcc) {
1154 case V4L2_PIX_FMT_RGB565:
1155 case V4L2_PIX_FMT_ARGB555:
1156 case V4L2_PIX_FMT_ARGB444:
1157 case V4L2_PIX_FMT_ABGR32:
1158 case V4L2_PIX_FMT_XBGR32:
1159 /* if sensor format is RAW, we convert inside ISC */
1160 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1161 isc->try_config.bits_pipeline = CFA_ENABLE |
1162 WB_ENABLE | GAM_ENABLES;
1163 } else {
1164 isc->try_config.bits_pipeline = 0x0;
1166 break;
1167 case V4L2_PIX_FMT_YUV420:
1168 /* if sensor format is RAW, we convert inside ISC */
1169 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1170 isc->try_config.bits_pipeline = CFA_ENABLE |
1171 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1172 SUB420_ENABLE | SUB422_ENABLE | CBC_ENABLE;
1173 } else {
1174 isc->try_config.bits_pipeline = 0x0;
1176 break;
1177 case V4L2_PIX_FMT_YUV422P:
1178 /* if sensor format is RAW, we convert inside ISC */
1179 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1180 isc->try_config.bits_pipeline = CFA_ENABLE |
1181 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1182 SUB422_ENABLE | CBC_ENABLE;
1183 } else {
1184 isc->try_config.bits_pipeline = 0x0;
1186 break;
1187 case V4L2_PIX_FMT_YUYV:
1188 /* if sensor format is RAW, we convert inside ISC */
1189 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1190 isc->try_config.bits_pipeline = CFA_ENABLE |
1191 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1192 SUB422_ENABLE | CBC_ENABLE;
1193 } else {
1194 isc->try_config.bits_pipeline = 0x0;
1196 break;
1197 case V4L2_PIX_FMT_GREY:
1198 if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1199 /* if sensor format is RAW, we convert inside ISC */
1200 isc->try_config.bits_pipeline = CFA_ENABLE |
1201 CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1202 CBC_ENABLE;
1203 } else {
1204 isc->try_config.bits_pipeline = 0x0;
1206 break;
1207 default:
1208 isc->try_config.bits_pipeline = 0x0;
1210 return 0;
1213 static void isc_try_fse(struct isc_device *isc,
1214 struct v4l2_subdev_pad_config *pad_cfg)
1216 int ret;
1217 struct v4l2_subdev_frame_size_enum fse = {};
1220 * If we do not know yet which format the subdev is using, we cannot
1221 * do anything.
1223 if (!isc->try_config.sd_format)
1224 return;
1226 fse.code = isc->try_config.sd_format->mbus_code;
1227 fse.which = V4L2_SUBDEV_FORMAT_TRY;
1229 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1230 pad_cfg, &fse);
1232 * Attempt to obtain format size from subdev. If not available,
1233 * just use the maximum ISC can receive.
1235 if (ret) {
1236 pad_cfg->try_crop.width = ISC_MAX_SUPPORT_WIDTH;
1237 pad_cfg->try_crop.height = ISC_MAX_SUPPORT_HEIGHT;
1238 } else {
1239 pad_cfg->try_crop.width = fse.max_width;
1240 pad_cfg->try_crop.height = fse.max_height;
1244 static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
1245 u32 *code)
1247 int i;
1248 struct isc_format *sd_fmt = NULL, *direct_fmt = NULL;
1249 struct v4l2_pix_format *pixfmt = &f->fmt.pix;
1250 struct v4l2_subdev_pad_config pad_cfg = {};
1251 struct v4l2_subdev_format format = {
1252 .which = V4L2_SUBDEV_FORMAT_TRY,
1254 u32 mbus_code;
1255 int ret;
1256 bool rlp_dma_direct_dump = false;
1258 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1259 return -EINVAL;
1261 /* Step 1: find a RAW format that is supported */
1262 for (i = 0; i < isc->num_user_formats; i++) {
1263 if (ISC_IS_FORMAT_RAW(isc->user_formats[i]->mbus_code)) {
1264 sd_fmt = isc->user_formats[i];
1265 break;
1268 /* Step 2: We can continue with this RAW format, or we can look
1269 * for better: maybe sensor supports directly what we need.
1271 direct_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
1273 /* Step 3: We have both. We decide given the module parameter which
1274 * one to use.
1276 if (direct_fmt && sd_fmt && sensor_preferred)
1277 sd_fmt = direct_fmt;
1279 /* Step 4: we do not have RAW but we have a direct format. Use it. */
1280 if (direct_fmt && !sd_fmt)
1281 sd_fmt = direct_fmt;
1283 /* Step 5: if we are using a direct format, we need to package
1284 * everything as 8 bit data and just dump it
1286 if (sd_fmt == direct_fmt)
1287 rlp_dma_direct_dump = true;
1289 /* Step 6: We have no format. This can happen if the userspace
1290 * requests some weird/invalid format.
1291 * In this case, default to whatever we have
1293 if (!sd_fmt && !direct_fmt) {
1294 sd_fmt = isc->user_formats[isc->num_user_formats - 1];
1295 v4l2_dbg(1, debug, &isc->v4l2_dev,
1296 "Sensor not supporting %.4s, using %.4s\n",
1297 (char *)&pixfmt->pixelformat, (char *)&sd_fmt->fourcc);
1300 if (!sd_fmt) {
1301 ret = -EINVAL;
1302 goto isc_try_fmt_err;
1305 /* Step 7: Print out what we decided for debugging */
1306 v4l2_dbg(1, debug, &isc->v4l2_dev,
1307 "Preferring to have sensor using format %.4s\n",
1308 (char *)&sd_fmt->fourcc);
1310 /* Step 8: at this moment we decided which format the subdev will use */
1311 isc->try_config.sd_format = sd_fmt;
1313 /* Limit to Atmel ISC hardware capabilities */
1314 if (pixfmt->width > ISC_MAX_SUPPORT_WIDTH)
1315 pixfmt->width = ISC_MAX_SUPPORT_WIDTH;
1316 if (pixfmt->height > ISC_MAX_SUPPORT_HEIGHT)
1317 pixfmt->height = ISC_MAX_SUPPORT_HEIGHT;
1320 * The mbus format is the one the subdev outputs.
1321 * The pixels will be transferred in this format Sensor -> ISC
1323 mbus_code = sd_fmt->mbus_code;
1326 * Validate formats. If the required format is not OK, default to raw.
1329 isc->try_config.fourcc = pixfmt->pixelformat;
1331 if (isc_try_validate_formats(isc)) {
1332 pixfmt->pixelformat = isc->try_config.fourcc = sd_fmt->fourcc;
1333 /* Re-try to validate the new format */
1334 ret = isc_try_validate_formats(isc);
1335 if (ret)
1336 goto isc_try_fmt_err;
1339 ret = isc_try_configure_rlp_dma(isc, rlp_dma_direct_dump);
1340 if (ret)
1341 goto isc_try_fmt_err;
1343 ret = isc_try_configure_pipeline(isc);
1344 if (ret)
1345 goto isc_try_fmt_err;
1347 /* Obtain frame sizes if possible to have crop requirements ready */
1348 isc_try_fse(isc, &pad_cfg);
1350 v4l2_fill_mbus_format(&format.format, pixfmt, mbus_code);
1351 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
1352 &pad_cfg, &format);
1353 if (ret < 0)
1354 goto isc_try_fmt_subdev_err;
1356 v4l2_fill_pix_format(pixfmt, &format.format);
1358 pixfmt->field = V4L2_FIELD_NONE;
1359 pixfmt->bytesperline = (pixfmt->width * isc->try_config.bpp) >> 3;
1360 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
1362 if (code)
1363 *code = mbus_code;
1365 return 0;
1367 isc_try_fmt_err:
1368 v4l2_err(&isc->v4l2_dev, "Could not find any possible format for a working pipeline\n");
1369 isc_try_fmt_subdev_err:
1370 memset(&isc->try_config, 0, sizeof(isc->try_config));
1372 return ret;
1375 static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
1377 struct v4l2_subdev_format format = {
1378 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1380 u32 mbus_code = 0;
1381 int ret;
1383 ret = isc_try_fmt(isc, f, &mbus_code);
1384 if (ret)
1385 return ret;
1387 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, mbus_code);
1388 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1389 set_fmt, NULL, &format);
1390 if (ret < 0)
1391 return ret;
1393 isc->fmt = *f;
1395 if (isc->try_config.sd_format && isc->config.sd_format &&
1396 isc->try_config.sd_format != isc->config.sd_format) {
1397 isc->ctrls.hist_stat = HIST_INIT;
1398 isc_reset_awb_ctrls(isc);
1400 /* make the try configuration active */
1401 isc->config = isc->try_config;
1403 v4l2_dbg(1, debug, &isc->v4l2_dev, "New ISC configuration in place\n");
1405 return 0;
1408 static int isc_s_fmt_vid_cap(struct file *file, void *priv,
1409 struct v4l2_format *f)
1411 struct isc_device *isc = video_drvdata(file);
1413 if (vb2_is_streaming(&isc->vb2_vidq))
1414 return -EBUSY;
1416 return isc_set_fmt(isc, f);
1419 static int isc_try_fmt_vid_cap(struct file *file, void *priv,
1420 struct v4l2_format *f)
1422 struct isc_device *isc = video_drvdata(file);
1424 return isc_try_fmt(isc, f, NULL);
1427 static int isc_enum_input(struct file *file, void *priv,
1428 struct v4l2_input *inp)
1430 if (inp->index != 0)
1431 return -EINVAL;
1433 inp->type = V4L2_INPUT_TYPE_CAMERA;
1434 inp->std = 0;
1435 strscpy(inp->name, "Camera", sizeof(inp->name));
1437 return 0;
1440 static int isc_g_input(struct file *file, void *priv, unsigned int *i)
1442 *i = 0;
1444 return 0;
1447 static int isc_s_input(struct file *file, void *priv, unsigned int i)
1449 if (i > 0)
1450 return -EINVAL;
1452 return 0;
1455 static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1457 struct isc_device *isc = video_drvdata(file);
1459 return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1462 static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1464 struct isc_device *isc = video_drvdata(file);
1466 return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1469 static int isc_enum_framesizes(struct file *file, void *fh,
1470 struct v4l2_frmsizeenum *fsize)
1472 struct isc_device *isc = video_drvdata(file);
1473 struct v4l2_subdev_frame_size_enum fse = {
1474 .code = isc->config.sd_format->mbus_code,
1475 .index = fsize->index,
1476 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1478 int ret = -EINVAL;
1479 int i;
1481 for (i = 0; i < isc->num_user_formats; i++)
1482 if (isc->user_formats[i]->fourcc == fsize->pixel_format)
1483 ret = 0;
1485 for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1486 if (controller_formats[i].fourcc == fsize->pixel_format)
1487 ret = 0;
1489 if (ret)
1490 return ret;
1492 ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1493 NULL, &fse);
1494 if (ret)
1495 return ret;
1497 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1498 fsize->discrete.width = fse.max_width;
1499 fsize->discrete.height = fse.max_height;
1501 return 0;
1504 static int isc_enum_frameintervals(struct file *file, void *fh,
1505 struct v4l2_frmivalenum *fival)
1507 struct isc_device *isc = video_drvdata(file);
1508 struct v4l2_subdev_frame_interval_enum fie = {
1509 .code = isc->config.sd_format->mbus_code,
1510 .index = fival->index,
1511 .width = fival->width,
1512 .height = fival->height,
1513 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1515 int ret = -EINVAL;
1516 unsigned int i;
1518 for (i = 0; i < isc->num_user_formats; i++)
1519 if (isc->user_formats[i]->fourcc == fival->pixel_format)
1520 ret = 0;
1522 for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1523 if (controller_formats[i].fourcc == fival->pixel_format)
1524 ret = 0;
1526 if (ret)
1527 return ret;
1529 ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1530 enum_frame_interval, NULL, &fie);
1531 if (ret)
1532 return ret;
1534 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1535 fival->discrete = fie.interval;
1537 return 0;
1540 static const struct v4l2_ioctl_ops isc_ioctl_ops = {
1541 .vidioc_querycap = isc_querycap,
1542 .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
1543 .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
1544 .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
1545 .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
1547 .vidioc_enum_input = isc_enum_input,
1548 .vidioc_g_input = isc_g_input,
1549 .vidioc_s_input = isc_s_input,
1551 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1552 .vidioc_querybuf = vb2_ioctl_querybuf,
1553 .vidioc_qbuf = vb2_ioctl_qbuf,
1554 .vidioc_expbuf = vb2_ioctl_expbuf,
1555 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1556 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1557 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1558 .vidioc_streamon = vb2_ioctl_streamon,
1559 .vidioc_streamoff = vb2_ioctl_streamoff,
1561 .vidioc_g_parm = isc_g_parm,
1562 .vidioc_s_parm = isc_s_parm,
1563 .vidioc_enum_framesizes = isc_enum_framesizes,
1564 .vidioc_enum_frameintervals = isc_enum_frameintervals,
1566 .vidioc_log_status = v4l2_ctrl_log_status,
1567 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1568 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1571 static int isc_open(struct file *file)
1573 struct isc_device *isc = video_drvdata(file);
1574 struct v4l2_subdev *sd = isc->current_subdev->sd;
1575 int ret;
1577 if (mutex_lock_interruptible(&isc->lock))
1578 return -ERESTARTSYS;
1580 ret = v4l2_fh_open(file);
1581 if (ret < 0)
1582 goto unlock;
1584 if (!v4l2_fh_is_singular_file(file))
1585 goto unlock;
1587 ret = v4l2_subdev_call(sd, core, s_power, 1);
1588 if (ret < 0 && ret != -ENOIOCTLCMD) {
1589 v4l2_fh_release(file);
1590 goto unlock;
1593 ret = isc_set_fmt(isc, &isc->fmt);
1594 if (ret) {
1595 v4l2_subdev_call(sd, core, s_power, 0);
1596 v4l2_fh_release(file);
1599 unlock:
1600 mutex_unlock(&isc->lock);
1601 return ret;
1604 static int isc_release(struct file *file)
1606 struct isc_device *isc = video_drvdata(file);
1607 struct v4l2_subdev *sd = isc->current_subdev->sd;
1608 bool fh_singular;
1609 int ret;
1611 mutex_lock(&isc->lock);
1613 fh_singular = v4l2_fh_is_singular_file(file);
1615 ret = _vb2_fop_release(file, NULL);
1617 if (fh_singular)
1618 v4l2_subdev_call(sd, core, s_power, 0);
1620 mutex_unlock(&isc->lock);
1622 return ret;
1625 static const struct v4l2_file_operations isc_fops = {
1626 .owner = THIS_MODULE,
1627 .open = isc_open,
1628 .release = isc_release,
1629 .unlocked_ioctl = video_ioctl2,
1630 .read = vb2_fop_read,
1631 .mmap = vb2_fop_mmap,
1632 .poll = vb2_fop_poll,
1635 irqreturn_t isc_interrupt(int irq, void *dev_id)
1637 struct isc_device *isc = (struct isc_device *)dev_id;
1638 struct regmap *regmap = isc->regmap;
1639 u32 isc_intsr, isc_intmask, pending;
1640 irqreturn_t ret = IRQ_NONE;
1642 regmap_read(regmap, ISC_INTSR, &isc_intsr);
1643 regmap_read(regmap, ISC_INTMASK, &isc_intmask);
1645 pending = isc_intsr & isc_intmask;
1647 if (likely(pending & ISC_INT_DDONE)) {
1648 spin_lock(&isc->dma_queue_lock);
1649 if (isc->cur_frm) {
1650 struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
1651 struct vb2_buffer *vb = &vbuf->vb2_buf;
1653 vb->timestamp = ktime_get_ns();
1654 vbuf->sequence = isc->sequence++;
1655 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1656 isc->cur_frm = NULL;
1659 if (!list_empty(&isc->dma_queue) && !isc->stop) {
1660 isc->cur_frm = list_first_entry(&isc->dma_queue,
1661 struct isc_buffer, list);
1662 list_del(&isc->cur_frm->list);
1664 isc_start_dma(isc);
1667 if (isc->stop)
1668 complete(&isc->comp);
1670 ret = IRQ_HANDLED;
1671 spin_unlock(&isc->dma_queue_lock);
1674 if (pending & ISC_INT_HISDONE) {
1675 schedule_work(&isc->awb_work);
1676 ret = IRQ_HANDLED;
1679 return ret;
1682 static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
1684 struct regmap *regmap = isc->regmap;
1685 struct isc_ctrls *ctrls = &isc->ctrls;
1686 u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
1687 u32 *hist_entry = &ctrls->hist_entry[0];
1688 u32 i;
1690 *min = 0;
1691 *max = HIST_ENTRIES;
1693 regmap_bulk_read(regmap, ISC_HIS_ENTRY, hist_entry, HIST_ENTRIES);
1695 *hist_count = 0;
1697 * we deliberately ignore the end of the histogram,
1698 * the most white pixels
1700 for (i = 1; i < HIST_ENTRIES; i++) {
1701 if (*hist_entry && !*min)
1702 *min = i;
1703 if (*hist_entry)
1704 *max = i;
1705 *hist_count += i * (*hist_entry++);
1708 if (!*min)
1709 *min = 1;
1712 static void isc_wb_update(struct isc_ctrls *ctrls)
1714 u32 *hist_count = &ctrls->hist_count[0];
1715 u32 c, offset[4];
1716 u64 avg = 0;
1717 /* We compute two gains, stretch gain and grey world gain */
1718 u32 s_gain[4], gw_gain[4];
1721 * According to Grey World, we need to set gains for R/B to normalize
1722 * them towards the green channel.
1723 * Thus we want to keep Green as fixed and adjust only Red/Blue
1724 * Compute the average of the both green channels first
1726 avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
1727 (u64)hist_count[ISC_HIS_CFG_MODE_GB];
1728 avg >>= 1;
1730 /* Green histogram is null, nothing to do */
1731 if (!avg)
1732 return;
1734 for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
1736 * the color offset is the minimum value of the histogram.
1737 * we stretch this color to the full range by substracting
1738 * this value from the color component.
1740 offset[c] = ctrls->hist_minmax[c][HIST_MIN_INDEX];
1742 * The offset is always at least 1. If the offset is 1, we do
1743 * not need to adjust it, so our result must be zero.
1744 * the offset is computed in a histogram on 9 bits (0..512)
1745 * but the offset in register is based on
1746 * 12 bits pipeline (0..4096).
1747 * we need to shift with the 3 bits that the histogram is
1748 * ignoring
1750 ctrls->offset[c] = (offset[c] - 1) << 3;
1752 /* the offset is then taken and converted to 2's complements */
1753 if (!ctrls->offset[c])
1754 ctrls->offset[c] = ISC_WB_O_ZERO_VAL;
1757 * the stretch gain is the total number of histogram bins
1758 * divided by the actual range of color component (Max - Min)
1759 * If we compute gain like this, the actual color component
1760 * will be stretched to the full histogram.
1761 * We need to shift 9 bits for precision, we have 9 bits for
1762 * decimals
1764 s_gain[c] = (HIST_ENTRIES << 9) /
1765 (ctrls->hist_minmax[c][HIST_MAX_INDEX] -
1766 ctrls->hist_minmax[c][HIST_MIN_INDEX] + 1);
1769 * Now we have to compute the gain w.r.t. the average.
1770 * Add/lose gain to the component towards the average.
1771 * If it happens that the component is zero, use the
1772 * fixed point value : 1.0 gain.
1774 if (hist_count[c])
1775 gw_gain[c] = div_u64(avg << 9, hist_count[c]);
1776 else
1777 gw_gain[c] = 1 << 9;
1779 /* multiply both gains and adjust for decimals */
1780 ctrls->gain[c] = s_gain[c] * gw_gain[c];
1781 ctrls->gain[c] >>= 9;
1785 static void isc_awb_work(struct work_struct *w)
1787 struct isc_device *isc =
1788 container_of(w, struct isc_device, awb_work);
1789 struct regmap *regmap = isc->regmap;
1790 struct isc_ctrls *ctrls = &isc->ctrls;
1791 u32 hist_id = ctrls->hist_id;
1792 u32 baysel;
1793 unsigned long flags;
1794 u32 min, max;
1796 /* streaming is not active anymore */
1797 if (isc->stop)
1798 return;
1800 if (ctrls->hist_stat != HIST_ENABLED)
1801 return;
1803 isc_hist_count(isc, &min, &max);
1804 ctrls->hist_minmax[hist_id][HIST_MIN_INDEX] = min;
1805 ctrls->hist_minmax[hist_id][HIST_MAX_INDEX] = max;
1807 if (hist_id != ISC_HIS_CFG_MODE_B) {
1808 hist_id++;
1809 } else {
1810 isc_wb_update(ctrls);
1811 hist_id = ISC_HIS_CFG_MODE_GR;
1814 ctrls->hist_id = hist_id;
1815 baysel = isc->config.sd_format->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
1817 /* if no more auto white balance, reset controls. */
1818 if (ctrls->awb == ISC_WB_NONE)
1819 isc_reset_awb_ctrls(isc);
1821 pm_runtime_get_sync(isc->dev);
1824 * only update if we have all the required histograms and controls
1825 * if awb has been disabled, we need to reset registers as well.
1827 if (hist_id == ISC_HIS_CFG_MODE_GR || ctrls->awb == ISC_WB_NONE) {
1829 * It may happen that DMA Done IRQ will trigger while we are
1830 * updating white balance registers here.
1831 * In that case, only parts of the controls have been updated.
1832 * We can avoid that by locking the section.
1834 spin_lock_irqsave(&isc->awb_lock, flags);
1835 isc_update_awb_ctrls(isc);
1836 spin_unlock_irqrestore(&isc->awb_lock, flags);
1839 * if we are doing just the one time white balance adjustment,
1840 * we are basically done.
1842 if (ctrls->awb == ISC_WB_ONETIME) {
1843 v4l2_info(&isc->v4l2_dev,
1844 "Completed one time white-balance adjustment.\n");
1845 ctrls->awb = ISC_WB_NONE;
1848 regmap_write(regmap, ISC_HIS_CFG, hist_id | baysel | ISC_HIS_CFG_RAR);
1849 isc_update_profile(isc);
1850 /* if awb has been disabled, we don't need to start another histogram */
1851 if (ctrls->awb)
1852 regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
1854 pm_runtime_put_sync(isc->dev);
1857 static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
1859 struct isc_device *isc = container_of(ctrl->handler,
1860 struct isc_device, ctrls.handler);
1861 struct isc_ctrls *ctrls = &isc->ctrls;
1863 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1864 return 0;
1866 switch (ctrl->id) {
1867 case V4L2_CID_BRIGHTNESS:
1868 ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
1869 break;
1870 case V4L2_CID_CONTRAST:
1871 ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
1872 break;
1873 case V4L2_CID_GAMMA:
1874 ctrls->gamma_index = ctrl->val;
1875 break;
1876 case V4L2_CID_AUTO_WHITE_BALANCE:
1877 if (ctrl->val == 1)
1878 ctrls->awb = ISC_WB_AUTO;
1879 else
1880 ctrls->awb = ISC_WB_NONE;
1882 /* we did not configure ISC yet */
1883 if (!isc->config.sd_format)
1884 break;
1886 if (ctrls->hist_stat != HIST_ENABLED)
1887 isc_reset_awb_ctrls(isc);
1889 if (isc->ctrls.awb == ISC_WB_AUTO &&
1890 vb2_is_streaming(&isc->vb2_vidq) &&
1891 ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
1892 isc_set_histogram(isc, true);
1894 break;
1895 case V4L2_CID_DO_WHITE_BALANCE:
1896 /* if AWB is enabled, do nothing */
1897 if (ctrls->awb == ISC_WB_AUTO)
1898 return 0;
1900 ctrls->awb = ISC_WB_ONETIME;
1901 isc_set_histogram(isc, true);
1902 v4l2_dbg(1, debug, &isc->v4l2_dev,
1903 "One time white-balance started.\n");
1904 break;
1905 default:
1906 return -EINVAL;
1909 return 0;
1912 static const struct v4l2_ctrl_ops isc_ctrl_ops = {
1913 .s_ctrl = isc_s_ctrl,
1916 static int isc_ctrl_init(struct isc_device *isc)
1918 const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
1919 struct isc_ctrls *ctrls = &isc->ctrls;
1920 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1921 int ret;
1923 ctrls->hist_stat = HIST_INIT;
1924 isc_reset_awb_ctrls(isc);
1926 ret = v4l2_ctrl_handler_init(hdl, 5);
1927 if (ret < 0)
1928 return ret;
1930 ctrls->brightness = 0;
1931 ctrls->contrast = 256;
1933 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
1934 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -2048, 2047, 1, 256);
1935 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, GAMMA_MAX, 1, 2);
1936 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1938 /* do_white_balance is a button, so min,max,step,default are ignored */
1939 isc->do_wb_ctrl = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_DO_WHITE_BALANCE,
1940 0, 0, 0, 0);
1942 if (!isc->do_wb_ctrl) {
1943 ret = hdl->error;
1944 v4l2_ctrl_handler_free(hdl);
1945 return ret;
1948 v4l2_ctrl_activate(isc->do_wb_ctrl, false);
1950 v4l2_ctrl_handler_setup(hdl);
1952 return 0;
1955 static int isc_async_bound(struct v4l2_async_notifier *notifier,
1956 struct v4l2_subdev *subdev,
1957 struct v4l2_async_subdev *asd)
1959 struct isc_device *isc = container_of(notifier->v4l2_dev,
1960 struct isc_device, v4l2_dev);
1961 struct isc_subdev_entity *subdev_entity =
1962 container_of(notifier, struct isc_subdev_entity, notifier);
1964 if (video_is_registered(&isc->video_dev)) {
1965 v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
1966 return -EBUSY;
1969 subdev_entity->sd = subdev;
1971 return 0;
1974 static void isc_async_unbind(struct v4l2_async_notifier *notifier,
1975 struct v4l2_subdev *subdev,
1976 struct v4l2_async_subdev *asd)
1978 struct isc_device *isc = container_of(notifier->v4l2_dev,
1979 struct isc_device, v4l2_dev);
1980 cancel_work_sync(&isc->awb_work);
1981 video_unregister_device(&isc->video_dev);
1982 v4l2_ctrl_handler_free(&isc->ctrls.handler);
1985 static struct isc_format *find_format_by_code(unsigned int code, int *index)
1987 struct isc_format *fmt = &formats_list[0];
1988 unsigned int i;
1990 for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
1991 if (fmt->mbus_code == code) {
1992 *index = i;
1993 return fmt;
1996 fmt++;
1999 return NULL;
2002 static int isc_formats_init(struct isc_device *isc)
2004 struct isc_format *fmt;
2005 struct v4l2_subdev *subdev = isc->current_subdev->sd;
2006 unsigned int num_fmts, i, j;
2007 u32 list_size = ARRAY_SIZE(formats_list);
2008 struct v4l2_subdev_mbus_code_enum mbus_code = {
2009 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2012 num_fmts = 0;
2013 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
2014 NULL, &mbus_code)) {
2015 mbus_code.index++;
2017 fmt = find_format_by_code(mbus_code.code, &i);
2018 if (!fmt) {
2019 v4l2_warn(&isc->v4l2_dev, "Mbus code %x not supported\n",
2020 mbus_code.code);
2021 continue;
2024 fmt->sd_support = true;
2025 num_fmts++;
2028 if (!num_fmts)
2029 return -ENXIO;
2031 isc->num_user_formats = num_fmts;
2032 isc->user_formats = devm_kcalloc(isc->dev,
2033 num_fmts, sizeof(*isc->user_formats),
2034 GFP_KERNEL);
2035 if (!isc->user_formats)
2036 return -ENOMEM;
2038 fmt = &formats_list[0];
2039 for (i = 0, j = 0; i < list_size; i++) {
2040 if (fmt->sd_support)
2041 isc->user_formats[j++] = fmt;
2042 fmt++;
2045 return 0;
2048 static int isc_set_default_fmt(struct isc_device *isc)
2050 struct v4l2_format f = {
2051 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2052 .fmt.pix = {
2053 .width = VGA_WIDTH,
2054 .height = VGA_HEIGHT,
2055 .field = V4L2_FIELD_NONE,
2056 .pixelformat = isc->user_formats[0]->fourcc,
2059 int ret;
2061 ret = isc_try_fmt(isc, &f, NULL);
2062 if (ret)
2063 return ret;
2065 isc->fmt = f;
2066 return 0;
2069 static int isc_async_complete(struct v4l2_async_notifier *notifier)
2071 struct isc_device *isc = container_of(notifier->v4l2_dev,
2072 struct isc_device, v4l2_dev);
2073 struct video_device *vdev = &isc->video_dev;
2074 struct vb2_queue *q = &isc->vb2_vidq;
2075 int ret = 0;
2077 INIT_WORK(&isc->awb_work, isc_awb_work);
2079 ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
2080 if (ret < 0) {
2081 v4l2_err(&isc->v4l2_dev, "Failed to register subdev nodes\n");
2082 return ret;
2085 isc->current_subdev = container_of(notifier,
2086 struct isc_subdev_entity, notifier);
2087 mutex_init(&isc->lock);
2088 init_completion(&isc->comp);
2090 /* Initialize videobuf2 queue */
2091 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2092 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2093 q->drv_priv = isc;
2094 q->buf_struct_size = sizeof(struct isc_buffer);
2095 q->ops = &isc_vb2_ops;
2096 q->mem_ops = &vb2_dma_contig_memops;
2097 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2098 q->lock = &isc->lock;
2099 q->min_buffers_needed = 1;
2100 q->dev = isc->dev;
2102 ret = vb2_queue_init(q);
2103 if (ret < 0) {
2104 v4l2_err(&isc->v4l2_dev,
2105 "vb2_queue_init() failed: %d\n", ret);
2106 goto isc_async_complete_err;
2109 /* Init video dma queues */
2110 INIT_LIST_HEAD(&isc->dma_queue);
2111 spin_lock_init(&isc->dma_queue_lock);
2112 spin_lock_init(&isc->awb_lock);
2114 ret = isc_formats_init(isc);
2115 if (ret < 0) {
2116 v4l2_err(&isc->v4l2_dev,
2117 "Init format failed: %d\n", ret);
2118 goto isc_async_complete_err;
2121 ret = isc_set_default_fmt(isc);
2122 if (ret) {
2123 v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
2124 goto isc_async_complete_err;
2127 ret = isc_ctrl_init(isc);
2128 if (ret) {
2129 v4l2_err(&isc->v4l2_dev, "Init isc ctrols failed: %d\n", ret);
2130 goto isc_async_complete_err;
2133 /* Register video device */
2134 strscpy(vdev->name, ATMEL_ISC_NAME, sizeof(vdev->name));
2135 vdev->release = video_device_release_empty;
2136 vdev->fops = &isc_fops;
2137 vdev->ioctl_ops = &isc_ioctl_ops;
2138 vdev->v4l2_dev = &isc->v4l2_dev;
2139 vdev->vfl_dir = VFL_DIR_RX;
2140 vdev->queue = q;
2141 vdev->lock = &isc->lock;
2142 vdev->ctrl_handler = &isc->ctrls.handler;
2143 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
2144 video_set_drvdata(vdev, isc);
2146 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
2147 if (ret < 0) {
2148 v4l2_err(&isc->v4l2_dev,
2149 "video_register_device failed: %d\n", ret);
2150 goto isc_async_complete_err;
2153 return 0;
2155 isc_async_complete_err:
2156 mutex_destroy(&isc->lock);
2157 return ret;
2160 const struct v4l2_async_notifier_operations isc_async_ops = {
2161 .bound = isc_async_bound,
2162 .unbind = isc_async_unbind,
2163 .complete = isc_async_complete,
2166 void isc_subdev_cleanup(struct isc_device *isc)
2168 struct isc_subdev_entity *subdev_entity;
2170 list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
2171 v4l2_async_notifier_unregister(&subdev_entity->notifier);
2172 v4l2_async_notifier_cleanup(&subdev_entity->notifier);
2175 INIT_LIST_HEAD(&isc->subdev_entities);
2178 int isc_pipeline_init(struct isc_device *isc)
2180 struct device *dev = isc->dev;
2181 struct regmap *regmap = isc->regmap;
2182 struct regmap_field *regs;
2183 unsigned int i;
2185 /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
2186 const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
2187 REG_FIELD(ISC_WB_CTRL, 0, 0),
2188 REG_FIELD(ISC_CFA_CTRL, 0, 0),
2189 REG_FIELD(ISC_CC_CTRL, 0, 0),
2190 REG_FIELD(ISC_GAM_CTRL, 0, 0),
2191 REG_FIELD(ISC_GAM_CTRL, 1, 1),
2192 REG_FIELD(ISC_GAM_CTRL, 2, 2),
2193 REG_FIELD(ISC_GAM_CTRL, 3, 3),
2194 REG_FIELD(ISC_CSC_CTRL, 0, 0),
2195 REG_FIELD(ISC_CBC_CTRL, 0, 0),
2196 REG_FIELD(ISC_SUB422_CTRL, 0, 0),
2197 REG_FIELD(ISC_SUB420_CTRL, 0, 0),
2200 for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
2201 regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
2202 if (IS_ERR(regs))
2203 return PTR_ERR(regs);
2205 isc->pipeline[i] = regs;
2208 return 0;
2211 /* regmap configuration */
2212 #define ATMEL_ISC_REG_MAX 0xbfc
2213 const struct regmap_config isc_regmap_config = {
2214 .reg_bits = 32,
2215 .reg_stride = 4,
2216 .val_bits = 32,
2217 .max_register = ATMEL_ISC_REG_MAX,