1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Microchip Image Sensor Controller (ISC) common driver base
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2016-2019 Microchip Technology, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Songjun Wu
8*4882a593Smuzhiyun * Author: Eugen Hristev <eugen.hristev@microchip.com>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/clk.h>
13*4882a593Smuzhiyun #include <linux/clkdev.h>
14*4882a593Smuzhiyun #include <linux/clk-provider.h>
15*4882a593Smuzhiyun #include <linux/delay.h>
16*4882a593Smuzhiyun #include <linux/interrupt.h>
17*4882a593Smuzhiyun #include <linux/math64.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/of.h>
20*4882a593Smuzhiyun #include <linux/of_graph.h>
21*4882a593Smuzhiyun #include <linux/platform_device.h>
22*4882a593Smuzhiyun #include <linux/pm_runtime.h>
23*4882a593Smuzhiyun #include <linux/regmap.h>
24*4882a593Smuzhiyun #include <linux/videodev2.h>
25*4882a593Smuzhiyun #include <linux/atmel-isc-media.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
28*4882a593Smuzhiyun #include <media/v4l2-device.h>
29*4882a593Smuzhiyun #include <media/v4l2-event.h>
30*4882a593Smuzhiyun #include <media/v4l2-image-sizes.h>
31*4882a593Smuzhiyun #include <media/v4l2-ioctl.h>
32*4882a593Smuzhiyun #include <media/v4l2-fwnode.h>
33*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
34*4882a593Smuzhiyun #include <media/videobuf2-dma-contig.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include "atmel-isc-regs.h"
37*4882a593Smuzhiyun #include "atmel-isc.h"
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun static unsigned int debug;
40*4882a593Smuzhiyun module_param(debug, int, 0644);
41*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "debug level (0-2)");
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun static unsigned int sensor_preferred = 1;
44*4882a593Smuzhiyun module_param(sensor_preferred, uint, 0644);
45*4882a593Smuzhiyun MODULE_PARM_DESC(sensor_preferred,
46*4882a593Smuzhiyun "Sensor is preferred to output the specified format (1-on 0-off), default 1");
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* This is a list of the formats that the ISC can *output* */
49*4882a593Smuzhiyun const struct isc_format controller_formats[] = {
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_ARGB444,
52*4882a593Smuzhiyun },
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_ARGB555,
55*4882a593Smuzhiyun },
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_RGB565,
58*4882a593Smuzhiyun },
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_ABGR32,
61*4882a593Smuzhiyun },
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_XBGR32,
64*4882a593Smuzhiyun },
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_YUV420,
67*4882a593Smuzhiyun },
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_YUYV,
70*4882a593Smuzhiyun },
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_YUV422P,
73*4882a593Smuzhiyun },
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_GREY,
76*4882a593Smuzhiyun },
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_Y10,
79*4882a593Smuzhiyun },
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* This is a list of formats that the ISC can receive as *input* */
83*4882a593Smuzhiyun struct isc_format formats_list[] = {
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SBGGR8,
86*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
87*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
88*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_BGBG,
89*4882a593Smuzhiyun },
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SGBRG8,
92*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8,
93*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
94*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_GBGB,
95*4882a593Smuzhiyun },
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SGRBG8,
98*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
99*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
100*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_GRGR,
101*4882a593Smuzhiyun },
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SRGGB8,
104*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8,
105*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
106*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_RGRG,
107*4882a593Smuzhiyun },
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SBGGR10,
110*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
111*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
112*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_RGRG,
113*4882a593Smuzhiyun },
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SGBRG10,
116*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10,
117*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
118*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_GBGB,
119*4882a593Smuzhiyun },
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SGRBG10,
122*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
123*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
124*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_GRGR,
125*4882a593Smuzhiyun },
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SRGGB10,
128*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
129*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
130*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_RGRG,
131*4882a593Smuzhiyun },
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SBGGR12,
134*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SBGGR12_1X12,
135*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
136*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_BGBG,
137*4882a593Smuzhiyun },
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SGBRG12,
140*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SGBRG12_1X12,
141*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
142*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_GBGB,
143*4882a593Smuzhiyun },
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SGRBG12,
146*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
147*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
148*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_GRGR,
149*4882a593Smuzhiyun },
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_SRGGB12,
152*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_SRGGB12_1X12,
153*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TWELVE,
154*4882a593Smuzhiyun .cfa_baycfg = ISC_BAY_CFG_RGRG,
155*4882a593Smuzhiyun },
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_GREY,
158*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_Y8_1X8,
159*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
160*4882a593Smuzhiyun },
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_YUYV,
163*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
164*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
165*4882a593Smuzhiyun },
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_RGB565,
168*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
169*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFE_CFG0_BPS_EIGHT,
170*4882a593Smuzhiyun },
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun .fourcc = V4L2_PIX_FMT_Y10,
173*4882a593Smuzhiyun .mbus_code = MEDIA_BUS_FMT_Y10_1X10,
174*4882a593Smuzhiyun .pfe_cfg0_bps = ISC_PFG_CFG0_BPS_TEN,
175*4882a593Smuzhiyun },
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun };
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* Gamma table with gamma 1/2.2 */
180*4882a593Smuzhiyun const u32 isc_gamma_table[GAMMA_MAX + 1][GAMMA_ENTRIES] = {
181*4882a593Smuzhiyun /* 0 --> gamma 1/1.8 */
182*4882a593Smuzhiyun { 0x65, 0x66002F, 0x950025, 0xBB0020, 0xDB001D, 0xF8001A,
183*4882a593Smuzhiyun 0x1130018, 0x12B0017, 0x1420016, 0x1580014, 0x16D0013, 0x1810012,
184*4882a593Smuzhiyun 0x1940012, 0x1A60012, 0x1B80011, 0x1C90010, 0x1DA0010, 0x1EA000F,
185*4882a593Smuzhiyun 0x1FA000F, 0x209000F, 0x218000F, 0x227000E, 0x235000E, 0x243000E,
186*4882a593Smuzhiyun 0x251000E, 0x25F000D, 0x26C000D, 0x279000D, 0x286000D, 0x293000C,
187*4882a593Smuzhiyun 0x2A0000C, 0x2AC000C, 0x2B8000C, 0x2C4000C, 0x2D0000B, 0x2DC000B,
188*4882a593Smuzhiyun 0x2E7000B, 0x2F3000B, 0x2FE000B, 0x309000B, 0x314000B, 0x31F000A,
189*4882a593Smuzhiyun 0x32A000A, 0x334000B, 0x33F000A, 0x349000A, 0x354000A, 0x35E000A,
190*4882a593Smuzhiyun 0x368000A, 0x372000A, 0x37C000A, 0x386000A, 0x3900009, 0x399000A,
191*4882a593Smuzhiyun 0x3A30009, 0x3AD0009, 0x3B60009, 0x3BF000A, 0x3C90009, 0x3D20009,
192*4882a593Smuzhiyun 0x3DB0009, 0x3E40009, 0x3ED0009, 0x3F60009 },
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* 1 --> gamma 1/2 */
195*4882a593Smuzhiyun { 0x7F, 0x800034, 0xB50028, 0xDE0021, 0x100001E, 0x11E001B,
196*4882a593Smuzhiyun 0x1390019, 0x1520017, 0x16A0015, 0x1800014, 0x1940014, 0x1A80013,
197*4882a593Smuzhiyun 0x1BB0012, 0x1CD0011, 0x1DF0010, 0x1EF0010, 0x200000F, 0x20F000F,
198*4882a593Smuzhiyun 0x21F000E, 0x22D000F, 0x23C000E, 0x24A000E, 0x258000D, 0x265000D,
199*4882a593Smuzhiyun 0x273000C, 0x27F000D, 0x28C000C, 0x299000C, 0x2A5000C, 0x2B1000B,
200*4882a593Smuzhiyun 0x2BC000C, 0x2C8000B, 0x2D3000C, 0x2DF000B, 0x2EA000A, 0x2F5000A,
201*4882a593Smuzhiyun 0x2FF000B, 0x30A000A, 0x314000B, 0x31F000A, 0x329000A, 0x333000A,
202*4882a593Smuzhiyun 0x33D0009, 0x3470009, 0x350000A, 0x35A0009, 0x363000A, 0x36D0009,
203*4882a593Smuzhiyun 0x3760009, 0x37F0009, 0x3880009, 0x3910009, 0x39A0009, 0x3A30009,
204*4882a593Smuzhiyun 0x3AC0008, 0x3B40009, 0x3BD0008, 0x3C60008, 0x3CE0008, 0x3D60009,
205*4882a593Smuzhiyun 0x3DF0008, 0x3E70008, 0x3EF0008, 0x3F70008 },
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* 2 --> gamma 1/2.2 */
208*4882a593Smuzhiyun { 0x99, 0x9B0038, 0xD4002A, 0xFF0023, 0x122001F, 0x141001B,
209*4882a593Smuzhiyun 0x15D0019, 0x1760017, 0x18E0015, 0x1A30015, 0x1B80013, 0x1CC0012,
210*4882a593Smuzhiyun 0x1DE0011, 0x1F00010, 0x2010010, 0x2110010, 0x221000F, 0x230000F,
211*4882a593Smuzhiyun 0x23F000E, 0x24D000E, 0x25B000D, 0x269000C, 0x276000C, 0x283000C,
212*4882a593Smuzhiyun 0x28F000C, 0x29B000C, 0x2A7000C, 0x2B3000B, 0x2BF000B, 0x2CA000B,
213*4882a593Smuzhiyun 0x2D5000B, 0x2E0000A, 0x2EB000A, 0x2F5000A, 0x2FF000A, 0x30A000A,
214*4882a593Smuzhiyun 0x3140009, 0x31E0009, 0x327000A, 0x3310009, 0x33A0009, 0x3440009,
215*4882a593Smuzhiyun 0x34D0009, 0x3560009, 0x35F0009, 0x3680008, 0x3710008, 0x3790009,
216*4882a593Smuzhiyun 0x3820008, 0x38A0008, 0x3930008, 0x39B0008, 0x3A30008, 0x3AB0008,
217*4882a593Smuzhiyun 0x3B30008, 0x3BB0008, 0x3C30008, 0x3CB0007, 0x3D20008, 0x3DA0007,
218*4882a593Smuzhiyun 0x3E20007, 0x3E90007, 0x3F00008, 0x3F80007 },
219*4882a593Smuzhiyun };
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun #define ISC_IS_FORMAT_RAW(mbus_code) \
222*4882a593Smuzhiyun (((mbus_code) & 0xf000) == 0x3000)
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun #define ISC_IS_FORMAT_GREY(mbus_code) \
225*4882a593Smuzhiyun (((mbus_code) == MEDIA_BUS_FMT_Y10_1X10) | \
226*4882a593Smuzhiyun (((mbus_code) == MEDIA_BUS_FMT_Y8_1X8)))
227*4882a593Smuzhiyun
isc_update_v4l2_ctrls(struct isc_device * isc)228*4882a593Smuzhiyun static inline void isc_update_v4l2_ctrls(struct isc_device *isc)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* In here we set the v4l2 controls w.r.t. our pipeline config */
233*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->r_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_R]);
234*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->b_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_B]);
235*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->gr_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GR]);
236*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->gb_gain_ctrl, ctrls->gain[ISC_HIS_CFG_MODE_GB]);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->r_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_R]);
239*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->b_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_B]);
240*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->gr_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GR]);
241*4882a593Smuzhiyun v4l2_ctrl_s_ctrl(isc->gb_off_ctrl, ctrls->offset[ISC_HIS_CFG_MODE_GB]);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
isc_update_awb_ctrls(struct isc_device * isc)244*4882a593Smuzhiyun static inline void isc_update_awb_ctrls(struct isc_device *isc)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* In here we set our actual hw pipeline config */
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun regmap_write(isc->regmap, ISC_WB_O_RGR,
251*4882a593Smuzhiyun ((ctrls->offset[ISC_HIS_CFG_MODE_R])) |
252*4882a593Smuzhiyun ((ctrls->offset[ISC_HIS_CFG_MODE_GR]) << 16));
253*4882a593Smuzhiyun regmap_write(isc->regmap, ISC_WB_O_BGB,
254*4882a593Smuzhiyun ((ctrls->offset[ISC_HIS_CFG_MODE_B])) |
255*4882a593Smuzhiyun ((ctrls->offset[ISC_HIS_CFG_MODE_GB]) << 16));
256*4882a593Smuzhiyun regmap_write(isc->regmap, ISC_WB_G_RGR,
257*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_R] |
258*4882a593Smuzhiyun (ctrls->gain[ISC_HIS_CFG_MODE_GR] << 16));
259*4882a593Smuzhiyun regmap_write(isc->regmap, ISC_WB_G_BGB,
260*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_B] |
261*4882a593Smuzhiyun (ctrls->gain[ISC_HIS_CFG_MODE_GB] << 16));
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
isc_reset_awb_ctrls(struct isc_device * isc)264*4882a593Smuzhiyun static inline void isc_reset_awb_ctrls(struct isc_device *isc)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun unsigned int c;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
269*4882a593Smuzhiyun /* gains have a fixed point at 9 decimals */
270*4882a593Smuzhiyun isc->ctrls.gain[c] = 1 << 9;
271*4882a593Smuzhiyun /* offsets are in 2's complements */
272*4882a593Smuzhiyun isc->ctrls.offset[c] = 0;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
isc_wait_clk_stable(struct clk_hw * hw)276*4882a593Smuzhiyun static int isc_wait_clk_stable(struct clk_hw *hw)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
279*4882a593Smuzhiyun struct regmap *regmap = isc_clk->regmap;
280*4882a593Smuzhiyun unsigned long timeout = jiffies + usecs_to_jiffies(1000);
281*4882a593Smuzhiyun unsigned int status;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun while (time_before(jiffies, timeout)) {
284*4882a593Smuzhiyun regmap_read(regmap, ISC_CLKSR, &status);
285*4882a593Smuzhiyun if (!(status & ISC_CLKSR_SIP))
286*4882a593Smuzhiyun return 0;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun usleep_range(10, 250);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun return -ETIMEDOUT;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
isc_clk_prepare(struct clk_hw * hw)294*4882a593Smuzhiyun static int isc_clk_prepare(struct clk_hw *hw)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun if (isc_clk->id == ISC_ISPCK)
299*4882a593Smuzhiyun pm_runtime_get_sync(isc_clk->dev);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun return isc_wait_clk_stable(hw);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun
isc_clk_unprepare(struct clk_hw * hw)304*4882a593Smuzhiyun static void isc_clk_unprepare(struct clk_hw *hw)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun isc_wait_clk_stable(hw);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (isc_clk->id == ISC_ISPCK)
311*4882a593Smuzhiyun pm_runtime_put_sync(isc_clk->dev);
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
isc_clk_enable(struct clk_hw * hw)314*4882a593Smuzhiyun static int isc_clk_enable(struct clk_hw *hw)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
317*4882a593Smuzhiyun u32 id = isc_clk->id;
318*4882a593Smuzhiyun struct regmap *regmap = isc_clk->regmap;
319*4882a593Smuzhiyun unsigned long flags;
320*4882a593Smuzhiyun unsigned int status;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun dev_dbg(isc_clk->dev, "ISC CLK: %s, div = %d, parent id = %d\n",
323*4882a593Smuzhiyun __func__, isc_clk->div, isc_clk->parent_id);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun spin_lock_irqsave(&isc_clk->lock, flags);
326*4882a593Smuzhiyun regmap_update_bits(regmap, ISC_CLKCFG,
327*4882a593Smuzhiyun ISC_CLKCFG_DIV_MASK(id) | ISC_CLKCFG_SEL_MASK(id),
328*4882a593Smuzhiyun (isc_clk->div << ISC_CLKCFG_DIV_SHIFT(id)) |
329*4882a593Smuzhiyun (isc_clk->parent_id << ISC_CLKCFG_SEL_SHIFT(id)));
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun regmap_write(regmap, ISC_CLKEN, ISC_CLK(id));
332*4882a593Smuzhiyun spin_unlock_irqrestore(&isc_clk->lock, flags);
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun regmap_read(regmap, ISC_CLKSR, &status);
335*4882a593Smuzhiyun if (status & ISC_CLK(id))
336*4882a593Smuzhiyun return 0;
337*4882a593Smuzhiyun else
338*4882a593Smuzhiyun return -EINVAL;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
isc_clk_disable(struct clk_hw * hw)341*4882a593Smuzhiyun static void isc_clk_disable(struct clk_hw *hw)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
344*4882a593Smuzhiyun u32 id = isc_clk->id;
345*4882a593Smuzhiyun unsigned long flags;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun spin_lock_irqsave(&isc_clk->lock, flags);
348*4882a593Smuzhiyun regmap_write(isc_clk->regmap, ISC_CLKDIS, ISC_CLK(id));
349*4882a593Smuzhiyun spin_unlock_irqrestore(&isc_clk->lock, flags);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
isc_clk_is_enabled(struct clk_hw * hw)352*4882a593Smuzhiyun static int isc_clk_is_enabled(struct clk_hw *hw)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
355*4882a593Smuzhiyun u32 status;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun if (isc_clk->id == ISC_ISPCK)
358*4882a593Smuzhiyun pm_runtime_get_sync(isc_clk->dev);
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun regmap_read(isc_clk->regmap, ISC_CLKSR, &status);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if (isc_clk->id == ISC_ISPCK)
363*4882a593Smuzhiyun pm_runtime_put_sync(isc_clk->dev);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun return status & ISC_CLK(isc_clk->id) ? 1 : 0;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun static unsigned long
isc_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)369*4882a593Smuzhiyun isc_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun return DIV_ROUND_CLOSEST(parent_rate, isc_clk->div + 1);
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
isc_clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)376*4882a593Smuzhiyun static int isc_clk_determine_rate(struct clk_hw *hw,
377*4882a593Smuzhiyun struct clk_rate_request *req)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
380*4882a593Smuzhiyun long best_rate = -EINVAL;
381*4882a593Smuzhiyun int best_diff = -1;
382*4882a593Smuzhiyun unsigned int i, div;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
385*4882a593Smuzhiyun struct clk_hw *parent;
386*4882a593Smuzhiyun unsigned long parent_rate;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun parent = clk_hw_get_parent_by_index(hw, i);
389*4882a593Smuzhiyun if (!parent)
390*4882a593Smuzhiyun continue;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun parent_rate = clk_hw_get_rate(parent);
393*4882a593Smuzhiyun if (!parent_rate)
394*4882a593Smuzhiyun continue;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun for (div = 1; div < ISC_CLK_MAX_DIV + 2; div++) {
397*4882a593Smuzhiyun unsigned long rate;
398*4882a593Smuzhiyun int diff;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun rate = DIV_ROUND_CLOSEST(parent_rate, div);
401*4882a593Smuzhiyun diff = abs(req->rate - rate);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun if (best_diff < 0 || best_diff > diff) {
404*4882a593Smuzhiyun best_rate = rate;
405*4882a593Smuzhiyun best_diff = diff;
406*4882a593Smuzhiyun req->best_parent_rate = parent_rate;
407*4882a593Smuzhiyun req->best_parent_hw = parent;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun if (!best_diff || rate < req->rate)
411*4882a593Smuzhiyun break;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun if (!best_diff)
415*4882a593Smuzhiyun break;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun dev_dbg(isc_clk->dev,
419*4882a593Smuzhiyun "ISC CLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
420*4882a593Smuzhiyun __func__, best_rate,
421*4882a593Smuzhiyun __clk_get_name((req->best_parent_hw)->clk),
422*4882a593Smuzhiyun req->best_parent_rate);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun if (best_rate < 0)
425*4882a593Smuzhiyun return best_rate;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun req->rate = best_rate;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun return 0;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
isc_clk_set_parent(struct clk_hw * hw,u8 index)432*4882a593Smuzhiyun static int isc_clk_set_parent(struct clk_hw *hw, u8 index)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun if (index >= clk_hw_get_num_parents(hw))
437*4882a593Smuzhiyun return -EINVAL;
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun isc_clk->parent_id = index;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun return 0;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
isc_clk_get_parent(struct clk_hw * hw)444*4882a593Smuzhiyun static u8 isc_clk_get_parent(struct clk_hw *hw)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun return isc_clk->parent_id;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
isc_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)451*4882a593Smuzhiyun static int isc_clk_set_rate(struct clk_hw *hw,
452*4882a593Smuzhiyun unsigned long rate,
453*4882a593Smuzhiyun unsigned long parent_rate)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun struct isc_clk *isc_clk = to_isc_clk(hw);
456*4882a593Smuzhiyun u32 div;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun if (!rate)
459*4882a593Smuzhiyun return -EINVAL;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun div = DIV_ROUND_CLOSEST(parent_rate, rate);
462*4882a593Smuzhiyun if (div > (ISC_CLK_MAX_DIV + 1) || !div)
463*4882a593Smuzhiyun return -EINVAL;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun isc_clk->div = div - 1;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun return 0;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun static const struct clk_ops isc_clk_ops = {
471*4882a593Smuzhiyun .prepare = isc_clk_prepare,
472*4882a593Smuzhiyun .unprepare = isc_clk_unprepare,
473*4882a593Smuzhiyun .enable = isc_clk_enable,
474*4882a593Smuzhiyun .disable = isc_clk_disable,
475*4882a593Smuzhiyun .is_enabled = isc_clk_is_enabled,
476*4882a593Smuzhiyun .recalc_rate = isc_clk_recalc_rate,
477*4882a593Smuzhiyun .determine_rate = isc_clk_determine_rate,
478*4882a593Smuzhiyun .set_parent = isc_clk_set_parent,
479*4882a593Smuzhiyun .get_parent = isc_clk_get_parent,
480*4882a593Smuzhiyun .set_rate = isc_clk_set_rate,
481*4882a593Smuzhiyun };
482*4882a593Smuzhiyun
isc_clk_register(struct isc_device * isc,unsigned int id)483*4882a593Smuzhiyun static int isc_clk_register(struct isc_device *isc, unsigned int id)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
486*4882a593Smuzhiyun struct device_node *np = isc->dev->of_node;
487*4882a593Smuzhiyun struct isc_clk *isc_clk;
488*4882a593Smuzhiyun struct clk_init_data init;
489*4882a593Smuzhiyun const char *clk_name = np->name;
490*4882a593Smuzhiyun const char *parent_names[3];
491*4882a593Smuzhiyun int num_parents;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun num_parents = of_clk_get_parent_count(np);
494*4882a593Smuzhiyun if (num_parents < 1 || num_parents > 3)
495*4882a593Smuzhiyun return -EINVAL;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun if (num_parents > 2 && id == ISC_ISPCK)
498*4882a593Smuzhiyun num_parents = 2;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun of_clk_parent_fill(np, parent_names, num_parents);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun if (id == ISC_MCK)
503*4882a593Smuzhiyun of_property_read_string(np, "clock-output-names", &clk_name);
504*4882a593Smuzhiyun else
505*4882a593Smuzhiyun clk_name = "isc-ispck";
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun init.parent_names = parent_names;
508*4882a593Smuzhiyun init.num_parents = num_parents;
509*4882a593Smuzhiyun init.name = clk_name;
510*4882a593Smuzhiyun init.ops = &isc_clk_ops;
511*4882a593Smuzhiyun init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun isc_clk = &isc->isc_clks[id];
514*4882a593Smuzhiyun isc_clk->hw.init = &init;
515*4882a593Smuzhiyun isc_clk->regmap = regmap;
516*4882a593Smuzhiyun isc_clk->id = id;
517*4882a593Smuzhiyun isc_clk->dev = isc->dev;
518*4882a593Smuzhiyun spin_lock_init(&isc_clk->lock);
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun isc_clk->clk = clk_register(isc->dev, &isc_clk->hw);
521*4882a593Smuzhiyun if (IS_ERR(isc_clk->clk)) {
522*4882a593Smuzhiyun dev_err(isc->dev, "%s: clock register fail\n", clk_name);
523*4882a593Smuzhiyun return PTR_ERR(isc_clk->clk);
524*4882a593Smuzhiyun } else if (id == ISC_MCK)
525*4882a593Smuzhiyun of_clk_add_provider(np, of_clk_src_simple_get, isc_clk->clk);
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun return 0;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
isc_clk_init(struct isc_device * isc)530*4882a593Smuzhiyun int isc_clk_init(struct isc_device *isc)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun unsigned int i;
533*4882a593Smuzhiyun int ret;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++)
536*4882a593Smuzhiyun isc->isc_clks[i].clk = ERR_PTR(-EINVAL);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
539*4882a593Smuzhiyun ret = isc_clk_register(isc, i);
540*4882a593Smuzhiyun if (ret)
541*4882a593Smuzhiyun return ret;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun return 0;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
isc_clk_cleanup(struct isc_device * isc)547*4882a593Smuzhiyun void isc_clk_cleanup(struct isc_device *isc)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun unsigned int i;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun of_clk_del_provider(isc->dev->of_node);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(isc->isc_clks); i++) {
554*4882a593Smuzhiyun struct isc_clk *isc_clk = &isc->isc_clks[i];
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun if (!IS_ERR(isc_clk->clk))
557*4882a593Smuzhiyun clk_unregister(isc_clk->clk);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
isc_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])561*4882a593Smuzhiyun static int isc_queue_setup(struct vb2_queue *vq,
562*4882a593Smuzhiyun unsigned int *nbuffers, unsigned int *nplanes,
563*4882a593Smuzhiyun unsigned int sizes[], struct device *alloc_devs[])
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun struct isc_device *isc = vb2_get_drv_priv(vq);
566*4882a593Smuzhiyun unsigned int size = isc->fmt.fmt.pix.sizeimage;
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun if (*nplanes)
569*4882a593Smuzhiyun return sizes[0] < size ? -EINVAL : 0;
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun *nplanes = 1;
572*4882a593Smuzhiyun sizes[0] = size;
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun return 0;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun
isc_buffer_prepare(struct vb2_buffer * vb)577*4882a593Smuzhiyun static int isc_buffer_prepare(struct vb2_buffer *vb)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
580*4882a593Smuzhiyun struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
581*4882a593Smuzhiyun unsigned long size = isc->fmt.fmt.pix.sizeimage;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun if (vb2_plane_size(vb, 0) < size) {
584*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "buffer too small (%lu < %lu)\n",
585*4882a593Smuzhiyun vb2_plane_size(vb, 0), size);
586*4882a593Smuzhiyun return -EINVAL;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun vb2_set_plane_payload(vb, 0, size);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun vbuf->field = isc->fmt.fmt.pix.field;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun return 0;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
isc_start_dma(struct isc_device * isc)596*4882a593Smuzhiyun static void isc_start_dma(struct isc_device *isc)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
599*4882a593Smuzhiyun u32 sizeimage = isc->fmt.fmt.pix.sizeimage;
600*4882a593Smuzhiyun u32 dctrl_dview;
601*4882a593Smuzhiyun dma_addr_t addr0;
602*4882a593Smuzhiyun u32 h, w;
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun h = isc->fmt.fmt.pix.height;
605*4882a593Smuzhiyun w = isc->fmt.fmt.pix.width;
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun * In case the sensor is not RAW, it will output a pixel (12-16 bits)
609*4882a593Smuzhiyun * with two samples on the ISC Data bus (which is 8-12)
610*4882a593Smuzhiyun * ISC will count each sample, so, we need to multiply these values
611*4882a593Smuzhiyun * by two, to get the real number of samples for the required pixels.
612*4882a593Smuzhiyun */
613*4882a593Smuzhiyun if (!ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code)) {
614*4882a593Smuzhiyun h <<= 1;
615*4882a593Smuzhiyun w <<= 1;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun /*
619*4882a593Smuzhiyun * We limit the column/row count that the ISC will output according
620*4882a593Smuzhiyun * to the configured resolution that we want.
621*4882a593Smuzhiyun * This will avoid the situation where the sensor is misconfigured,
622*4882a593Smuzhiyun * sending more data, and the ISC will just take it and DMA to memory,
623*4882a593Smuzhiyun * causing corruption.
624*4882a593Smuzhiyun */
625*4882a593Smuzhiyun regmap_write(regmap, ISC_PFE_CFG1,
626*4882a593Smuzhiyun (ISC_PFE_CFG1_COLMIN(0) & ISC_PFE_CFG1_COLMIN_MASK) |
627*4882a593Smuzhiyun (ISC_PFE_CFG1_COLMAX(w - 1) & ISC_PFE_CFG1_COLMAX_MASK));
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun regmap_write(regmap, ISC_PFE_CFG2,
630*4882a593Smuzhiyun (ISC_PFE_CFG2_ROWMIN(0) & ISC_PFE_CFG2_ROWMIN_MASK) |
631*4882a593Smuzhiyun (ISC_PFE_CFG2_ROWMAX(h - 1) & ISC_PFE_CFG2_ROWMAX_MASK));
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun regmap_update_bits(regmap, ISC_PFE_CFG0,
634*4882a593Smuzhiyun ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN,
635*4882a593Smuzhiyun ISC_PFE_CFG0_COLEN | ISC_PFE_CFG0_ROWEN);
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun addr0 = vb2_dma_contig_plane_dma_addr(&isc->cur_frm->vb.vb2_buf, 0);
638*4882a593Smuzhiyun regmap_write(regmap, ISC_DAD0, addr0);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun switch (isc->config.fourcc) {
641*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV420:
642*4882a593Smuzhiyun regmap_write(regmap, ISC_DAD1, addr0 + (sizeimage * 2) / 3);
643*4882a593Smuzhiyun regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 5) / 6);
644*4882a593Smuzhiyun break;
645*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV422P:
646*4882a593Smuzhiyun regmap_write(regmap, ISC_DAD1, addr0 + sizeimage / 2);
647*4882a593Smuzhiyun regmap_write(regmap, ISC_DAD2, addr0 + (sizeimage * 3) / 4);
648*4882a593Smuzhiyun break;
649*4882a593Smuzhiyun default:
650*4882a593Smuzhiyun break;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun dctrl_dview = isc->config.dctrl_dview;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun regmap_write(regmap, ISC_DCTRL, dctrl_dview | ISC_DCTRL_IE_IS);
656*4882a593Smuzhiyun spin_lock(&isc->awb_lock);
657*4882a593Smuzhiyun regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_CAPTURE);
658*4882a593Smuzhiyun spin_unlock(&isc->awb_lock);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
isc_set_pipeline(struct isc_device * isc,u32 pipeline)661*4882a593Smuzhiyun static void isc_set_pipeline(struct isc_device *isc, u32 pipeline)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
664*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
665*4882a593Smuzhiyun u32 val, bay_cfg;
666*4882a593Smuzhiyun const u32 *gamma;
667*4882a593Smuzhiyun unsigned int i;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
670*4882a593Smuzhiyun for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
671*4882a593Smuzhiyun val = pipeline & BIT(i) ? 1 : 0;
672*4882a593Smuzhiyun regmap_field_write(isc->pipeline[i], val);
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (!pipeline)
676*4882a593Smuzhiyun return;
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun bay_cfg = isc->config.sd_format->cfa_baycfg;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun regmap_write(regmap, ISC_WB_CFG, bay_cfg);
681*4882a593Smuzhiyun isc_update_awb_ctrls(isc);
682*4882a593Smuzhiyun isc_update_v4l2_ctrls(isc);
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun regmap_write(regmap, ISC_CFA_CFG, bay_cfg | ISC_CFA_CFG_EITPOL);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun gamma = &isc_gamma_table[ctrls->gamma_index][0];
687*4882a593Smuzhiyun regmap_bulk_write(regmap, ISC_GAM_BENTRY, gamma, GAMMA_ENTRIES);
688*4882a593Smuzhiyun regmap_bulk_write(regmap, ISC_GAM_GENTRY, gamma, GAMMA_ENTRIES);
689*4882a593Smuzhiyun regmap_bulk_write(regmap, ISC_GAM_RENTRY, gamma, GAMMA_ENTRIES);
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun /* Convert RGB to YUV */
692*4882a593Smuzhiyun regmap_write(regmap, ISC_CSC_YR_YG, 0x42 | (0x81 << 16));
693*4882a593Smuzhiyun regmap_write(regmap, ISC_CSC_YB_OY, 0x19 | (0x10 << 16));
694*4882a593Smuzhiyun regmap_write(regmap, ISC_CSC_CBR_CBG, 0xFDA | (0xFB6 << 16));
695*4882a593Smuzhiyun regmap_write(regmap, ISC_CSC_CBB_OCB, 0x70 | (0x80 << 16));
696*4882a593Smuzhiyun regmap_write(regmap, ISC_CSC_CRR_CRG, 0x70 | (0xFA2 << 16));
697*4882a593Smuzhiyun regmap_write(regmap, ISC_CSC_CRB_OCR, 0xFEE | (0x80 << 16));
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun regmap_write(regmap, ISC_CBC_BRIGHT, ctrls->brightness);
700*4882a593Smuzhiyun regmap_write(regmap, ISC_CBC_CONTRAST, ctrls->contrast);
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
isc_update_profile(struct isc_device * isc)703*4882a593Smuzhiyun static int isc_update_profile(struct isc_device *isc)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
706*4882a593Smuzhiyun u32 sr;
707*4882a593Smuzhiyun int counter = 100;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_UPPRO);
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun regmap_read(regmap, ISC_CTRLSR, &sr);
712*4882a593Smuzhiyun while ((sr & ISC_CTRL_UPPRO) && counter--) {
713*4882a593Smuzhiyun usleep_range(1000, 2000);
714*4882a593Smuzhiyun regmap_read(regmap, ISC_CTRLSR, &sr);
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun if (counter < 0) {
718*4882a593Smuzhiyun v4l2_warn(&isc->v4l2_dev, "Time out to update profile\n");
719*4882a593Smuzhiyun return -ETIMEDOUT;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun return 0;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
isc_set_histogram(struct isc_device * isc,bool enable)725*4882a593Smuzhiyun static void isc_set_histogram(struct isc_device *isc, bool enable)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
728*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun if (enable) {
731*4882a593Smuzhiyun regmap_write(regmap, ISC_HIS_CFG,
732*4882a593Smuzhiyun ISC_HIS_CFG_MODE_GR |
733*4882a593Smuzhiyun (isc->config.sd_format->cfa_baycfg
734*4882a593Smuzhiyun << ISC_HIS_CFG_BAYSEL_SHIFT) |
735*4882a593Smuzhiyun ISC_HIS_CFG_RAR);
736*4882a593Smuzhiyun regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_EN);
737*4882a593Smuzhiyun regmap_write(regmap, ISC_INTEN, ISC_INT_HISDONE);
738*4882a593Smuzhiyun ctrls->hist_id = ISC_HIS_CFG_MODE_GR;
739*4882a593Smuzhiyun isc_update_profile(isc);
740*4882a593Smuzhiyun regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun ctrls->hist_stat = HIST_ENABLED;
743*4882a593Smuzhiyun } else {
744*4882a593Smuzhiyun regmap_write(regmap, ISC_INTDIS, ISC_INT_HISDONE);
745*4882a593Smuzhiyun regmap_write(regmap, ISC_HIS_CTRL, ISC_HIS_CTRL_DIS);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun ctrls->hist_stat = HIST_DISABLED;
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun
isc_configure(struct isc_device * isc)751*4882a593Smuzhiyun static int isc_configure(struct isc_device *isc)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
754*4882a593Smuzhiyun u32 pfe_cfg0, rlp_mode, dcfg, mask, pipeline;
755*4882a593Smuzhiyun struct isc_subdev_entity *subdev = isc->current_subdev;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun pfe_cfg0 = isc->config.sd_format->pfe_cfg0_bps;
758*4882a593Smuzhiyun rlp_mode = isc->config.rlp_cfg_mode;
759*4882a593Smuzhiyun pipeline = isc->config.bits_pipeline;
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun dcfg = isc->config.dcfg_imode |
762*4882a593Smuzhiyun ISC_DCFG_YMBSIZE_BEATS8 | ISC_DCFG_CMBSIZE_BEATS8;
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun pfe_cfg0 |= subdev->pfe_cfg0 | ISC_PFE_CFG0_MODE_PROGRESSIVE;
765*4882a593Smuzhiyun mask = ISC_PFE_CFG0_BPS_MASK | ISC_PFE_CFG0_HPOL_LOW |
766*4882a593Smuzhiyun ISC_PFE_CFG0_VPOL_LOW | ISC_PFE_CFG0_PPOL_LOW |
767*4882a593Smuzhiyun ISC_PFE_CFG0_MODE_MASK | ISC_PFE_CFG0_CCIR_CRC |
768*4882a593Smuzhiyun ISC_PFE_CFG0_CCIR656;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun regmap_update_bits(regmap, ISC_PFE_CFG0, mask, pfe_cfg0);
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun regmap_update_bits(regmap, ISC_RLP_CFG, ISC_RLP_CFG_MODE_MASK,
773*4882a593Smuzhiyun rlp_mode);
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun regmap_write(regmap, ISC_DCFG, dcfg);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /* Set the pipeline */
778*4882a593Smuzhiyun isc_set_pipeline(isc, pipeline);
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun /*
781*4882a593Smuzhiyun * The current implemented histogram is available for RAW R, B, GB, GR
782*4882a593Smuzhiyun * channels. We need to check if sensor is outputting RAW BAYER
783*4882a593Smuzhiyun */
784*4882a593Smuzhiyun if (isc->ctrls.awb &&
785*4882a593Smuzhiyun ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
786*4882a593Smuzhiyun isc_set_histogram(isc, true);
787*4882a593Smuzhiyun else
788*4882a593Smuzhiyun isc_set_histogram(isc, false);
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /* Update profile */
791*4882a593Smuzhiyun return isc_update_profile(isc);
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
isc_start_streaming(struct vb2_queue * vq,unsigned int count)794*4882a593Smuzhiyun static int isc_start_streaming(struct vb2_queue *vq, unsigned int count)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun struct isc_device *isc = vb2_get_drv_priv(vq);
797*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
798*4882a593Smuzhiyun struct isc_buffer *buf;
799*4882a593Smuzhiyun unsigned long flags;
800*4882a593Smuzhiyun int ret;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun /* Enable stream on the sub device */
803*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 1);
804*4882a593Smuzhiyun if (ret && ret != -ENOIOCTLCMD) {
805*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "stream on failed in subdev %d\n",
806*4882a593Smuzhiyun ret);
807*4882a593Smuzhiyun goto err_start_stream;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun pm_runtime_get_sync(isc->dev);
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun ret = isc_configure(isc);
813*4882a593Smuzhiyun if (unlikely(ret))
814*4882a593Smuzhiyun goto err_configure;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun /* Enable DMA interrupt */
817*4882a593Smuzhiyun regmap_write(regmap, ISC_INTEN, ISC_INT_DDONE);
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun spin_lock_irqsave(&isc->dma_queue_lock, flags);
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun isc->sequence = 0;
822*4882a593Smuzhiyun isc->stop = false;
823*4882a593Smuzhiyun reinit_completion(&isc->comp);
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun isc->cur_frm = list_first_entry(&isc->dma_queue,
826*4882a593Smuzhiyun struct isc_buffer, list);
827*4882a593Smuzhiyun list_del(&isc->cur_frm->list);
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun isc_start_dma(isc);
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /* if we streaming from RAW, we can do one-shot white balance adj */
834*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
835*4882a593Smuzhiyun v4l2_ctrl_activate(isc->do_wb_ctrl, true);
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun return 0;
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun err_configure:
840*4882a593Smuzhiyun pm_runtime_put_sync(isc->dev);
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun err_start_stream:
845*4882a593Smuzhiyun spin_lock_irqsave(&isc->dma_queue_lock, flags);
846*4882a593Smuzhiyun list_for_each_entry(buf, &isc->dma_queue, list)
847*4882a593Smuzhiyun vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
848*4882a593Smuzhiyun INIT_LIST_HEAD(&isc->dma_queue);
849*4882a593Smuzhiyun spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun return ret;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun
isc_stop_streaming(struct vb2_queue * vq)854*4882a593Smuzhiyun static void isc_stop_streaming(struct vb2_queue *vq)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun struct isc_device *isc = vb2_get_drv_priv(vq);
857*4882a593Smuzhiyun unsigned long flags;
858*4882a593Smuzhiyun struct isc_buffer *buf;
859*4882a593Smuzhiyun int ret;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun v4l2_ctrl_activate(isc->do_wb_ctrl, false);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun isc->stop = true;
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /* Wait until the end of the current frame */
866*4882a593Smuzhiyun if (isc->cur_frm && !wait_for_completion_timeout(&isc->comp, 5 * HZ))
867*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev,
868*4882a593Smuzhiyun "Timeout waiting for end of the capture\n");
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun /* Disable DMA interrupt */
871*4882a593Smuzhiyun regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE);
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun pm_runtime_put_sync(isc->dev);
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun /* Disable stream on the sub device */
876*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, video, s_stream, 0);
877*4882a593Smuzhiyun if (ret && ret != -ENOIOCTLCMD)
878*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "stream off failed in subdev\n");
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun /* Release all active buffers */
881*4882a593Smuzhiyun spin_lock_irqsave(&isc->dma_queue_lock, flags);
882*4882a593Smuzhiyun if (unlikely(isc->cur_frm)) {
883*4882a593Smuzhiyun vb2_buffer_done(&isc->cur_frm->vb.vb2_buf,
884*4882a593Smuzhiyun VB2_BUF_STATE_ERROR);
885*4882a593Smuzhiyun isc->cur_frm = NULL;
886*4882a593Smuzhiyun }
887*4882a593Smuzhiyun list_for_each_entry(buf, &isc->dma_queue, list)
888*4882a593Smuzhiyun vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
889*4882a593Smuzhiyun INIT_LIST_HEAD(&isc->dma_queue);
890*4882a593Smuzhiyun spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun
isc_buffer_queue(struct vb2_buffer * vb)893*4882a593Smuzhiyun static void isc_buffer_queue(struct vb2_buffer *vb)
894*4882a593Smuzhiyun {
895*4882a593Smuzhiyun struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
896*4882a593Smuzhiyun struct isc_buffer *buf = container_of(vbuf, struct isc_buffer, vb);
897*4882a593Smuzhiyun struct isc_device *isc = vb2_get_drv_priv(vb->vb2_queue);
898*4882a593Smuzhiyun unsigned long flags;
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun spin_lock_irqsave(&isc->dma_queue_lock, flags);
901*4882a593Smuzhiyun if (!isc->cur_frm && list_empty(&isc->dma_queue) &&
902*4882a593Smuzhiyun vb2_is_streaming(vb->vb2_queue)) {
903*4882a593Smuzhiyun isc->cur_frm = buf;
904*4882a593Smuzhiyun isc_start_dma(isc);
905*4882a593Smuzhiyun } else
906*4882a593Smuzhiyun list_add_tail(&buf->list, &isc->dma_queue);
907*4882a593Smuzhiyun spin_unlock_irqrestore(&isc->dma_queue_lock, flags);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun
find_format_by_fourcc(struct isc_device * isc,unsigned int fourcc)910*4882a593Smuzhiyun static struct isc_format *find_format_by_fourcc(struct isc_device *isc,
911*4882a593Smuzhiyun unsigned int fourcc)
912*4882a593Smuzhiyun {
913*4882a593Smuzhiyun unsigned int num_formats = isc->num_user_formats;
914*4882a593Smuzhiyun struct isc_format *fmt;
915*4882a593Smuzhiyun unsigned int i;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun for (i = 0; i < num_formats; i++) {
918*4882a593Smuzhiyun fmt = isc->user_formats[i];
919*4882a593Smuzhiyun if (fmt->fourcc == fourcc)
920*4882a593Smuzhiyun return fmt;
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun return NULL;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun static const struct vb2_ops isc_vb2_ops = {
927*4882a593Smuzhiyun .queue_setup = isc_queue_setup,
928*4882a593Smuzhiyun .wait_prepare = vb2_ops_wait_prepare,
929*4882a593Smuzhiyun .wait_finish = vb2_ops_wait_finish,
930*4882a593Smuzhiyun .buf_prepare = isc_buffer_prepare,
931*4882a593Smuzhiyun .start_streaming = isc_start_streaming,
932*4882a593Smuzhiyun .stop_streaming = isc_stop_streaming,
933*4882a593Smuzhiyun .buf_queue = isc_buffer_queue,
934*4882a593Smuzhiyun };
935*4882a593Smuzhiyun
isc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)936*4882a593Smuzhiyun static int isc_querycap(struct file *file, void *priv,
937*4882a593Smuzhiyun struct v4l2_capability *cap)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
940*4882a593Smuzhiyun
941*4882a593Smuzhiyun strscpy(cap->driver, ATMEL_ISC_NAME, sizeof(cap->driver));
942*4882a593Smuzhiyun strscpy(cap->card, "Atmel Image Sensor Controller", sizeof(cap->card));
943*4882a593Smuzhiyun snprintf(cap->bus_info, sizeof(cap->bus_info),
944*4882a593Smuzhiyun "platform:%s", isc->v4l2_dev.name);
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun return 0;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun
isc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)949*4882a593Smuzhiyun static int isc_enum_fmt_vid_cap(struct file *file, void *priv,
950*4882a593Smuzhiyun struct v4l2_fmtdesc *f)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun u32 index = f->index;
953*4882a593Smuzhiyun u32 i, supported_index;
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun if (index < ARRAY_SIZE(controller_formats)) {
956*4882a593Smuzhiyun f->pixelformat = controller_formats[index].fourcc;
957*4882a593Smuzhiyun return 0;
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun index -= ARRAY_SIZE(controller_formats);
961*4882a593Smuzhiyun
962*4882a593Smuzhiyun i = 0;
963*4882a593Smuzhiyun supported_index = 0;
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
966*4882a593Smuzhiyun if (!ISC_IS_FORMAT_RAW(formats_list[i].mbus_code) ||
967*4882a593Smuzhiyun !formats_list[i].sd_support)
968*4882a593Smuzhiyun continue;
969*4882a593Smuzhiyun if (supported_index == index) {
970*4882a593Smuzhiyun f->pixelformat = formats_list[i].fourcc;
971*4882a593Smuzhiyun return 0;
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun supported_index++;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun return -EINVAL;
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
isc_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * fmt)979*4882a593Smuzhiyun static int isc_g_fmt_vid_cap(struct file *file, void *priv,
980*4882a593Smuzhiyun struct v4l2_format *fmt)
981*4882a593Smuzhiyun {
982*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun *fmt = isc->fmt;
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun return 0;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /*
990*4882a593Smuzhiyun * Checks the current configured format, if ISC can output it,
991*4882a593Smuzhiyun * considering which type of format the ISC receives from the sensor
992*4882a593Smuzhiyun */
isc_try_validate_formats(struct isc_device * isc)993*4882a593Smuzhiyun static int isc_try_validate_formats(struct isc_device *isc)
994*4882a593Smuzhiyun {
995*4882a593Smuzhiyun int ret;
996*4882a593Smuzhiyun bool bayer = false, yuv = false, rgb = false, grey = false;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun /* all formats supported by the RLP module are OK */
999*4882a593Smuzhiyun switch (isc->try_config.fourcc) {
1000*4882a593Smuzhiyun case V4L2_PIX_FMT_SBGGR8:
1001*4882a593Smuzhiyun case V4L2_PIX_FMT_SGBRG8:
1002*4882a593Smuzhiyun case V4L2_PIX_FMT_SGRBG8:
1003*4882a593Smuzhiyun case V4L2_PIX_FMT_SRGGB8:
1004*4882a593Smuzhiyun case V4L2_PIX_FMT_SBGGR10:
1005*4882a593Smuzhiyun case V4L2_PIX_FMT_SGBRG10:
1006*4882a593Smuzhiyun case V4L2_PIX_FMT_SGRBG10:
1007*4882a593Smuzhiyun case V4L2_PIX_FMT_SRGGB10:
1008*4882a593Smuzhiyun case V4L2_PIX_FMT_SBGGR12:
1009*4882a593Smuzhiyun case V4L2_PIX_FMT_SGBRG12:
1010*4882a593Smuzhiyun case V4L2_PIX_FMT_SGRBG12:
1011*4882a593Smuzhiyun case V4L2_PIX_FMT_SRGGB12:
1012*4882a593Smuzhiyun ret = 0;
1013*4882a593Smuzhiyun bayer = true;
1014*4882a593Smuzhiyun break;
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV420:
1017*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV422P:
1018*4882a593Smuzhiyun case V4L2_PIX_FMT_YUYV:
1019*4882a593Smuzhiyun ret = 0;
1020*4882a593Smuzhiyun yuv = true;
1021*4882a593Smuzhiyun break;
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun case V4L2_PIX_FMT_RGB565:
1024*4882a593Smuzhiyun case V4L2_PIX_FMT_ABGR32:
1025*4882a593Smuzhiyun case V4L2_PIX_FMT_XBGR32:
1026*4882a593Smuzhiyun case V4L2_PIX_FMT_ARGB444:
1027*4882a593Smuzhiyun case V4L2_PIX_FMT_ARGB555:
1028*4882a593Smuzhiyun ret = 0;
1029*4882a593Smuzhiyun rgb = true;
1030*4882a593Smuzhiyun break;
1031*4882a593Smuzhiyun case V4L2_PIX_FMT_GREY:
1032*4882a593Smuzhiyun case V4L2_PIX_FMT_Y10:
1033*4882a593Smuzhiyun ret = 0;
1034*4882a593Smuzhiyun grey = true;
1035*4882a593Smuzhiyun break;
1036*4882a593Smuzhiyun default:
1037*4882a593Smuzhiyun /* any other different formats are not supported */
1038*4882a593Smuzhiyun ret = -EINVAL;
1039*4882a593Smuzhiyun }
1040*4882a593Smuzhiyun v4l2_dbg(1, debug, &isc->v4l2_dev,
1041*4882a593Smuzhiyun "Format validation, requested rgb=%u, yuv=%u, grey=%u, bayer=%u\n",
1042*4882a593Smuzhiyun rgb, yuv, grey, bayer);
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun /* we cannot output RAW if we do not receive RAW */
1045*4882a593Smuzhiyun if ((bayer) && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code))
1046*4882a593Smuzhiyun return -EINVAL;
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun /* we cannot output GREY if we do not receive RAW/GREY */
1049*4882a593Smuzhiyun if (grey && !ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code) &&
1050*4882a593Smuzhiyun !ISC_IS_FORMAT_GREY(isc->try_config.sd_format->mbus_code))
1051*4882a593Smuzhiyun return -EINVAL;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun return ret;
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /*
1057*4882a593Smuzhiyun * Configures the RLP and DMA modules, depending on the output format
1058*4882a593Smuzhiyun * configured for the ISC.
1059*4882a593Smuzhiyun * If direct_dump == true, just dump raw data 8/16 bits depending on format.
1060*4882a593Smuzhiyun */
isc_try_configure_rlp_dma(struct isc_device * isc,bool direct_dump)1061*4882a593Smuzhiyun static int isc_try_configure_rlp_dma(struct isc_device *isc, bool direct_dump)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun switch (isc->try_config.fourcc) {
1064*4882a593Smuzhiyun case V4L2_PIX_FMT_SBGGR8:
1065*4882a593Smuzhiyun case V4L2_PIX_FMT_SGBRG8:
1066*4882a593Smuzhiyun case V4L2_PIX_FMT_SGRBG8:
1067*4882a593Smuzhiyun case V4L2_PIX_FMT_SRGGB8:
1068*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1069*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1070*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1071*4882a593Smuzhiyun isc->try_config.bpp = 8;
1072*4882a593Smuzhiyun break;
1073*4882a593Smuzhiyun case V4L2_PIX_FMT_SBGGR10:
1074*4882a593Smuzhiyun case V4L2_PIX_FMT_SGBRG10:
1075*4882a593Smuzhiyun case V4L2_PIX_FMT_SGRBG10:
1076*4882a593Smuzhiyun case V4L2_PIX_FMT_SRGGB10:
1077*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT10;
1078*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1079*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1080*4882a593Smuzhiyun isc->try_config.bpp = 16;
1081*4882a593Smuzhiyun break;
1082*4882a593Smuzhiyun case V4L2_PIX_FMT_SBGGR12:
1083*4882a593Smuzhiyun case V4L2_PIX_FMT_SGBRG12:
1084*4882a593Smuzhiyun case V4L2_PIX_FMT_SGRBG12:
1085*4882a593Smuzhiyun case V4L2_PIX_FMT_SRGGB12:
1086*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT12;
1087*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1088*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1089*4882a593Smuzhiyun isc->try_config.bpp = 16;
1090*4882a593Smuzhiyun break;
1091*4882a593Smuzhiyun case V4L2_PIX_FMT_RGB565:
1092*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_RGB565;
1093*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1094*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1095*4882a593Smuzhiyun isc->try_config.bpp = 16;
1096*4882a593Smuzhiyun break;
1097*4882a593Smuzhiyun case V4L2_PIX_FMT_ARGB444:
1098*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB444;
1099*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1100*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1101*4882a593Smuzhiyun isc->try_config.bpp = 16;
1102*4882a593Smuzhiyun break;
1103*4882a593Smuzhiyun case V4L2_PIX_FMT_ARGB555:
1104*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB555;
1105*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1106*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1107*4882a593Smuzhiyun isc->try_config.bpp = 16;
1108*4882a593Smuzhiyun break;
1109*4882a593Smuzhiyun case V4L2_PIX_FMT_ABGR32:
1110*4882a593Smuzhiyun case V4L2_PIX_FMT_XBGR32:
1111*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_ARGB32;
1112*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1113*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1114*4882a593Smuzhiyun isc->try_config.bpp = 32;
1115*4882a593Smuzhiyun break;
1116*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV420:
1117*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1118*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC420P;
1119*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1120*4882a593Smuzhiyun isc->try_config.bpp = 12;
1121*4882a593Smuzhiyun break;
1122*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV422P:
1123*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1124*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_YC422P;
1125*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PLANAR;
1126*4882a593Smuzhiyun isc->try_config.bpp = 16;
1127*4882a593Smuzhiyun break;
1128*4882a593Smuzhiyun case V4L2_PIX_FMT_YUYV:
1129*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_YYCC;
1130*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED32;
1131*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1132*4882a593Smuzhiyun isc->try_config.bpp = 16;
1133*4882a593Smuzhiyun break;
1134*4882a593Smuzhiyun case V4L2_PIX_FMT_GREY:
1135*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY8;
1136*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1137*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1138*4882a593Smuzhiyun isc->try_config.bpp = 8;
1139*4882a593Smuzhiyun break;
1140*4882a593Smuzhiyun case V4L2_PIX_FMT_Y10:
1141*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DATY10;
1142*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED16;
1143*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1144*4882a593Smuzhiyun isc->try_config.bpp = 16;
1145*4882a593Smuzhiyun break;
1146*4882a593Smuzhiyun default:
1147*4882a593Smuzhiyun return -EINVAL;
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun if (direct_dump) {
1151*4882a593Smuzhiyun isc->try_config.rlp_cfg_mode = ISC_RLP_CFG_MODE_DAT8;
1152*4882a593Smuzhiyun isc->try_config.dcfg_imode = ISC_DCFG_IMODE_PACKED8;
1153*4882a593Smuzhiyun isc->try_config.dctrl_dview = ISC_DCTRL_DVIEW_PACKED;
1154*4882a593Smuzhiyun return 0;
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun return 0;
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun /*
1161*4882a593Smuzhiyun * Configuring pipeline modules, depending on which format the ISC outputs
1162*4882a593Smuzhiyun * and considering which format it has as input from the sensor.
1163*4882a593Smuzhiyun */
isc_try_configure_pipeline(struct isc_device * isc)1164*4882a593Smuzhiyun static int isc_try_configure_pipeline(struct isc_device *isc)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun switch (isc->try_config.fourcc) {
1167*4882a593Smuzhiyun case V4L2_PIX_FMT_RGB565:
1168*4882a593Smuzhiyun case V4L2_PIX_FMT_ARGB555:
1169*4882a593Smuzhiyun case V4L2_PIX_FMT_ARGB444:
1170*4882a593Smuzhiyun case V4L2_PIX_FMT_ABGR32:
1171*4882a593Smuzhiyun case V4L2_PIX_FMT_XBGR32:
1172*4882a593Smuzhiyun /* if sensor format is RAW, we convert inside ISC */
1173*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1174*4882a593Smuzhiyun isc->try_config.bits_pipeline = CFA_ENABLE |
1175*4882a593Smuzhiyun WB_ENABLE | GAM_ENABLES;
1176*4882a593Smuzhiyun } else {
1177*4882a593Smuzhiyun isc->try_config.bits_pipeline = 0x0;
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun break;
1180*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV420:
1181*4882a593Smuzhiyun /* if sensor format is RAW, we convert inside ISC */
1182*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1183*4882a593Smuzhiyun isc->try_config.bits_pipeline = CFA_ENABLE |
1184*4882a593Smuzhiyun CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1185*4882a593Smuzhiyun SUB420_ENABLE | SUB422_ENABLE | CBC_ENABLE;
1186*4882a593Smuzhiyun } else {
1187*4882a593Smuzhiyun isc->try_config.bits_pipeline = 0x0;
1188*4882a593Smuzhiyun }
1189*4882a593Smuzhiyun break;
1190*4882a593Smuzhiyun case V4L2_PIX_FMT_YUV422P:
1191*4882a593Smuzhiyun /* if sensor format is RAW, we convert inside ISC */
1192*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1193*4882a593Smuzhiyun isc->try_config.bits_pipeline = CFA_ENABLE |
1194*4882a593Smuzhiyun CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1195*4882a593Smuzhiyun SUB422_ENABLE | CBC_ENABLE;
1196*4882a593Smuzhiyun } else {
1197*4882a593Smuzhiyun isc->try_config.bits_pipeline = 0x0;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun break;
1200*4882a593Smuzhiyun case V4L2_PIX_FMT_YUYV:
1201*4882a593Smuzhiyun /* if sensor format is RAW, we convert inside ISC */
1202*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1203*4882a593Smuzhiyun isc->try_config.bits_pipeline = CFA_ENABLE |
1204*4882a593Smuzhiyun CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1205*4882a593Smuzhiyun SUB422_ENABLE | CBC_ENABLE;
1206*4882a593Smuzhiyun } else {
1207*4882a593Smuzhiyun isc->try_config.bits_pipeline = 0x0;
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun break;
1210*4882a593Smuzhiyun case V4L2_PIX_FMT_GREY:
1211*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->try_config.sd_format->mbus_code)) {
1212*4882a593Smuzhiyun /* if sensor format is RAW, we convert inside ISC */
1213*4882a593Smuzhiyun isc->try_config.bits_pipeline = CFA_ENABLE |
1214*4882a593Smuzhiyun CSC_ENABLE | WB_ENABLE | GAM_ENABLES |
1215*4882a593Smuzhiyun CBC_ENABLE;
1216*4882a593Smuzhiyun } else {
1217*4882a593Smuzhiyun isc->try_config.bits_pipeline = 0x0;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun break;
1220*4882a593Smuzhiyun default:
1221*4882a593Smuzhiyun isc->try_config.bits_pipeline = 0x0;
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun return 0;
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun
isc_try_fse(struct isc_device * isc,struct v4l2_subdev_pad_config * pad_cfg)1226*4882a593Smuzhiyun static void isc_try_fse(struct isc_device *isc,
1227*4882a593Smuzhiyun struct v4l2_subdev_pad_config *pad_cfg)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun int ret;
1230*4882a593Smuzhiyun struct v4l2_subdev_frame_size_enum fse = {};
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun /*
1233*4882a593Smuzhiyun * If we do not know yet which format the subdev is using, we cannot
1234*4882a593Smuzhiyun * do anything.
1235*4882a593Smuzhiyun */
1236*4882a593Smuzhiyun if (!isc->try_config.sd_format)
1237*4882a593Smuzhiyun return;
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun fse.code = isc->try_config.sd_format->mbus_code;
1240*4882a593Smuzhiyun fse.which = V4L2_SUBDEV_FORMAT_TRY;
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1243*4882a593Smuzhiyun pad_cfg, &fse);
1244*4882a593Smuzhiyun /*
1245*4882a593Smuzhiyun * Attempt to obtain format size from subdev. If not available,
1246*4882a593Smuzhiyun * just use the maximum ISC can receive.
1247*4882a593Smuzhiyun */
1248*4882a593Smuzhiyun if (ret) {
1249*4882a593Smuzhiyun pad_cfg->try_crop.width = ISC_MAX_SUPPORT_WIDTH;
1250*4882a593Smuzhiyun pad_cfg->try_crop.height = ISC_MAX_SUPPORT_HEIGHT;
1251*4882a593Smuzhiyun } else {
1252*4882a593Smuzhiyun pad_cfg->try_crop.width = fse.max_width;
1253*4882a593Smuzhiyun pad_cfg->try_crop.height = fse.max_height;
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun
isc_try_fmt(struct isc_device * isc,struct v4l2_format * f,u32 * code)1257*4882a593Smuzhiyun static int isc_try_fmt(struct isc_device *isc, struct v4l2_format *f,
1258*4882a593Smuzhiyun u32 *code)
1259*4882a593Smuzhiyun {
1260*4882a593Smuzhiyun int i;
1261*4882a593Smuzhiyun struct isc_format *sd_fmt = NULL, *direct_fmt = NULL;
1262*4882a593Smuzhiyun struct v4l2_pix_format *pixfmt = &f->fmt.pix;
1263*4882a593Smuzhiyun struct v4l2_subdev_pad_config pad_cfg = {};
1264*4882a593Smuzhiyun struct v4l2_subdev_format format = {
1265*4882a593Smuzhiyun .which = V4L2_SUBDEV_FORMAT_TRY,
1266*4882a593Smuzhiyun };
1267*4882a593Smuzhiyun u32 mbus_code;
1268*4882a593Smuzhiyun int ret;
1269*4882a593Smuzhiyun bool rlp_dma_direct_dump = false;
1270*4882a593Smuzhiyun
1271*4882a593Smuzhiyun if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1272*4882a593Smuzhiyun return -EINVAL;
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun /* Step 1: find a RAW format that is supported */
1275*4882a593Smuzhiyun for (i = 0; i < isc->num_user_formats; i++) {
1276*4882a593Smuzhiyun if (ISC_IS_FORMAT_RAW(isc->user_formats[i]->mbus_code)) {
1277*4882a593Smuzhiyun sd_fmt = isc->user_formats[i];
1278*4882a593Smuzhiyun break;
1279*4882a593Smuzhiyun }
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun /* Step 2: We can continue with this RAW format, or we can look
1282*4882a593Smuzhiyun * for better: maybe sensor supports directly what we need.
1283*4882a593Smuzhiyun */
1284*4882a593Smuzhiyun direct_fmt = find_format_by_fourcc(isc, pixfmt->pixelformat);
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun /* Step 3: We have both. We decide given the module parameter which
1287*4882a593Smuzhiyun * one to use.
1288*4882a593Smuzhiyun */
1289*4882a593Smuzhiyun if (direct_fmt && sd_fmt && sensor_preferred)
1290*4882a593Smuzhiyun sd_fmt = direct_fmt;
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun /* Step 4: we do not have RAW but we have a direct format. Use it. */
1293*4882a593Smuzhiyun if (direct_fmt && !sd_fmt)
1294*4882a593Smuzhiyun sd_fmt = direct_fmt;
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun /* Step 5: if we are using a direct format, we need to package
1297*4882a593Smuzhiyun * everything as 8 bit data and just dump it
1298*4882a593Smuzhiyun */
1299*4882a593Smuzhiyun if (sd_fmt == direct_fmt)
1300*4882a593Smuzhiyun rlp_dma_direct_dump = true;
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun /* Step 6: We have no format. This can happen if the userspace
1303*4882a593Smuzhiyun * requests some weird/invalid format.
1304*4882a593Smuzhiyun * In this case, default to whatever we have
1305*4882a593Smuzhiyun */
1306*4882a593Smuzhiyun if (!sd_fmt && !direct_fmt) {
1307*4882a593Smuzhiyun sd_fmt = isc->user_formats[isc->num_user_formats - 1];
1308*4882a593Smuzhiyun v4l2_dbg(1, debug, &isc->v4l2_dev,
1309*4882a593Smuzhiyun "Sensor not supporting %.4s, using %.4s\n",
1310*4882a593Smuzhiyun (char *)&pixfmt->pixelformat, (char *)&sd_fmt->fourcc);
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun if (!sd_fmt) {
1314*4882a593Smuzhiyun ret = -EINVAL;
1315*4882a593Smuzhiyun goto isc_try_fmt_err;
1316*4882a593Smuzhiyun }
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun /* Step 7: Print out what we decided for debugging */
1319*4882a593Smuzhiyun v4l2_dbg(1, debug, &isc->v4l2_dev,
1320*4882a593Smuzhiyun "Preferring to have sensor using format %.4s\n",
1321*4882a593Smuzhiyun (char *)&sd_fmt->fourcc);
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun /* Step 8: at this moment we decided which format the subdev will use */
1324*4882a593Smuzhiyun isc->try_config.sd_format = sd_fmt;
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun /* Limit to Atmel ISC hardware capabilities */
1327*4882a593Smuzhiyun if (pixfmt->width > ISC_MAX_SUPPORT_WIDTH)
1328*4882a593Smuzhiyun pixfmt->width = ISC_MAX_SUPPORT_WIDTH;
1329*4882a593Smuzhiyun if (pixfmt->height > ISC_MAX_SUPPORT_HEIGHT)
1330*4882a593Smuzhiyun pixfmt->height = ISC_MAX_SUPPORT_HEIGHT;
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun /*
1333*4882a593Smuzhiyun * The mbus format is the one the subdev outputs.
1334*4882a593Smuzhiyun * The pixels will be transferred in this format Sensor -> ISC
1335*4882a593Smuzhiyun */
1336*4882a593Smuzhiyun mbus_code = sd_fmt->mbus_code;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun /*
1339*4882a593Smuzhiyun * Validate formats. If the required format is not OK, default to raw.
1340*4882a593Smuzhiyun */
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun isc->try_config.fourcc = pixfmt->pixelformat;
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun if (isc_try_validate_formats(isc)) {
1345*4882a593Smuzhiyun pixfmt->pixelformat = isc->try_config.fourcc = sd_fmt->fourcc;
1346*4882a593Smuzhiyun /* Re-try to validate the new format */
1347*4882a593Smuzhiyun ret = isc_try_validate_formats(isc);
1348*4882a593Smuzhiyun if (ret)
1349*4882a593Smuzhiyun goto isc_try_fmt_err;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun ret = isc_try_configure_rlp_dma(isc, rlp_dma_direct_dump);
1353*4882a593Smuzhiyun if (ret)
1354*4882a593Smuzhiyun goto isc_try_fmt_err;
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun ret = isc_try_configure_pipeline(isc);
1357*4882a593Smuzhiyun if (ret)
1358*4882a593Smuzhiyun goto isc_try_fmt_err;
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun /* Obtain frame sizes if possible to have crop requirements ready */
1361*4882a593Smuzhiyun isc_try_fse(isc, &pad_cfg);
1362*4882a593Smuzhiyun
1363*4882a593Smuzhiyun v4l2_fill_mbus_format(&format.format, pixfmt, mbus_code);
1364*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, pad, set_fmt,
1365*4882a593Smuzhiyun &pad_cfg, &format);
1366*4882a593Smuzhiyun if (ret < 0)
1367*4882a593Smuzhiyun goto isc_try_fmt_subdev_err;
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun v4l2_fill_pix_format(pixfmt, &format.format);
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun pixfmt->field = V4L2_FIELD_NONE;
1372*4882a593Smuzhiyun pixfmt->bytesperline = (pixfmt->width * isc->try_config.bpp) >> 3;
1373*4882a593Smuzhiyun pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun if (code)
1376*4882a593Smuzhiyun *code = mbus_code;
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun return 0;
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun isc_try_fmt_err:
1381*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "Could not find any possible format for a working pipeline\n");
1382*4882a593Smuzhiyun isc_try_fmt_subdev_err:
1383*4882a593Smuzhiyun memset(&isc->try_config, 0, sizeof(isc->try_config));
1384*4882a593Smuzhiyun
1385*4882a593Smuzhiyun return ret;
1386*4882a593Smuzhiyun }
1387*4882a593Smuzhiyun
isc_set_fmt(struct isc_device * isc,struct v4l2_format * f)1388*4882a593Smuzhiyun static int isc_set_fmt(struct isc_device *isc, struct v4l2_format *f)
1389*4882a593Smuzhiyun {
1390*4882a593Smuzhiyun struct v4l2_subdev_format format = {
1391*4882a593Smuzhiyun .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1392*4882a593Smuzhiyun };
1393*4882a593Smuzhiyun u32 mbus_code = 0;
1394*4882a593Smuzhiyun int ret;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun ret = isc_try_fmt(isc, f, &mbus_code);
1397*4882a593Smuzhiyun if (ret)
1398*4882a593Smuzhiyun return ret;
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun v4l2_fill_mbus_format(&format.format, &f->fmt.pix, mbus_code);
1401*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1402*4882a593Smuzhiyun set_fmt, NULL, &format);
1403*4882a593Smuzhiyun if (ret < 0)
1404*4882a593Smuzhiyun return ret;
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun isc->fmt = *f;
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun if (isc->try_config.sd_format && isc->config.sd_format &&
1409*4882a593Smuzhiyun isc->try_config.sd_format != isc->config.sd_format) {
1410*4882a593Smuzhiyun isc->ctrls.hist_stat = HIST_INIT;
1411*4882a593Smuzhiyun isc_reset_awb_ctrls(isc);
1412*4882a593Smuzhiyun isc_update_v4l2_ctrls(isc);
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun /* make the try configuration active */
1415*4882a593Smuzhiyun isc->config = isc->try_config;
1416*4882a593Smuzhiyun
1417*4882a593Smuzhiyun v4l2_dbg(1, debug, &isc->v4l2_dev, "New ISC configuration in place\n");
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun return 0;
1420*4882a593Smuzhiyun }
1421*4882a593Smuzhiyun
isc_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1422*4882a593Smuzhiyun static int isc_s_fmt_vid_cap(struct file *file, void *priv,
1423*4882a593Smuzhiyun struct v4l2_format *f)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun if (vb2_is_streaming(&isc->vb2_vidq))
1428*4882a593Smuzhiyun return -EBUSY;
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun return isc_set_fmt(isc, f);
1431*4882a593Smuzhiyun }
1432*4882a593Smuzhiyun
isc_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1433*4882a593Smuzhiyun static int isc_try_fmt_vid_cap(struct file *file, void *priv,
1434*4882a593Smuzhiyun struct v4l2_format *f)
1435*4882a593Smuzhiyun {
1436*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun return isc_try_fmt(isc, f, NULL);
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun
isc_enum_input(struct file * file,void * priv,struct v4l2_input * inp)1441*4882a593Smuzhiyun static int isc_enum_input(struct file *file, void *priv,
1442*4882a593Smuzhiyun struct v4l2_input *inp)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun if (inp->index != 0)
1445*4882a593Smuzhiyun return -EINVAL;
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun inp->type = V4L2_INPUT_TYPE_CAMERA;
1448*4882a593Smuzhiyun inp->std = 0;
1449*4882a593Smuzhiyun strscpy(inp->name, "Camera", sizeof(inp->name));
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun return 0;
1452*4882a593Smuzhiyun }
1453*4882a593Smuzhiyun
isc_g_input(struct file * file,void * priv,unsigned int * i)1454*4882a593Smuzhiyun static int isc_g_input(struct file *file, void *priv, unsigned int *i)
1455*4882a593Smuzhiyun {
1456*4882a593Smuzhiyun *i = 0;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun return 0;
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun
isc_s_input(struct file * file,void * priv,unsigned int i)1461*4882a593Smuzhiyun static int isc_s_input(struct file *file, void *priv, unsigned int i)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun if (i > 0)
1464*4882a593Smuzhiyun return -EINVAL;
1465*4882a593Smuzhiyun
1466*4882a593Smuzhiyun return 0;
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun
isc_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1469*4882a593Smuzhiyun static int isc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1470*4882a593Smuzhiyun {
1471*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun return v4l2_g_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1474*4882a593Smuzhiyun }
1475*4882a593Smuzhiyun
isc_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1476*4882a593Smuzhiyun static int isc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun return v4l2_s_parm_cap(video_devdata(file), isc->current_subdev->sd, a);
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun
isc_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)1483*4882a593Smuzhiyun static int isc_enum_framesizes(struct file *file, void *fh,
1484*4882a593Smuzhiyun struct v4l2_frmsizeenum *fsize)
1485*4882a593Smuzhiyun {
1486*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1487*4882a593Smuzhiyun struct v4l2_subdev_frame_size_enum fse = {
1488*4882a593Smuzhiyun .code = isc->config.sd_format->mbus_code,
1489*4882a593Smuzhiyun .index = fsize->index,
1490*4882a593Smuzhiyun .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1491*4882a593Smuzhiyun };
1492*4882a593Smuzhiyun int ret = -EINVAL;
1493*4882a593Smuzhiyun int i;
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun for (i = 0; i < isc->num_user_formats; i++)
1496*4882a593Smuzhiyun if (isc->user_formats[i]->fourcc == fsize->pixel_format)
1497*4882a593Smuzhiyun ret = 0;
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1500*4882a593Smuzhiyun if (controller_formats[i].fourcc == fsize->pixel_format)
1501*4882a593Smuzhiyun ret = 0;
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun if (ret)
1504*4882a593Smuzhiyun return ret;
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, pad, enum_frame_size,
1507*4882a593Smuzhiyun NULL, &fse);
1508*4882a593Smuzhiyun if (ret)
1509*4882a593Smuzhiyun return ret;
1510*4882a593Smuzhiyun
1511*4882a593Smuzhiyun fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1512*4882a593Smuzhiyun fsize->discrete.width = fse.max_width;
1513*4882a593Smuzhiyun fsize->discrete.height = fse.max_height;
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun return 0;
1516*4882a593Smuzhiyun }
1517*4882a593Smuzhiyun
isc_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * fival)1518*4882a593Smuzhiyun static int isc_enum_frameintervals(struct file *file, void *fh,
1519*4882a593Smuzhiyun struct v4l2_frmivalenum *fival)
1520*4882a593Smuzhiyun {
1521*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1522*4882a593Smuzhiyun struct v4l2_subdev_frame_interval_enum fie = {
1523*4882a593Smuzhiyun .code = isc->config.sd_format->mbus_code,
1524*4882a593Smuzhiyun .index = fival->index,
1525*4882a593Smuzhiyun .width = fival->width,
1526*4882a593Smuzhiyun .height = fival->height,
1527*4882a593Smuzhiyun .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1528*4882a593Smuzhiyun };
1529*4882a593Smuzhiyun int ret = -EINVAL;
1530*4882a593Smuzhiyun unsigned int i;
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun for (i = 0; i < isc->num_user_formats; i++)
1533*4882a593Smuzhiyun if (isc->user_formats[i]->fourcc == fival->pixel_format)
1534*4882a593Smuzhiyun ret = 0;
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(controller_formats); i++)
1537*4882a593Smuzhiyun if (controller_formats[i].fourcc == fival->pixel_format)
1538*4882a593Smuzhiyun ret = 0;
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun if (ret)
1541*4882a593Smuzhiyun return ret;
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun ret = v4l2_subdev_call(isc->current_subdev->sd, pad,
1544*4882a593Smuzhiyun enum_frame_interval, NULL, &fie);
1545*4882a593Smuzhiyun if (ret)
1546*4882a593Smuzhiyun return ret;
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1549*4882a593Smuzhiyun fival->discrete = fie.interval;
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun return 0;
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun static const struct v4l2_ioctl_ops isc_ioctl_ops = {
1555*4882a593Smuzhiyun .vidioc_querycap = isc_querycap,
1556*4882a593Smuzhiyun .vidioc_enum_fmt_vid_cap = isc_enum_fmt_vid_cap,
1557*4882a593Smuzhiyun .vidioc_g_fmt_vid_cap = isc_g_fmt_vid_cap,
1558*4882a593Smuzhiyun .vidioc_s_fmt_vid_cap = isc_s_fmt_vid_cap,
1559*4882a593Smuzhiyun .vidioc_try_fmt_vid_cap = isc_try_fmt_vid_cap,
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun .vidioc_enum_input = isc_enum_input,
1562*4882a593Smuzhiyun .vidioc_g_input = isc_g_input,
1563*4882a593Smuzhiyun .vidioc_s_input = isc_s_input,
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun .vidioc_reqbufs = vb2_ioctl_reqbufs,
1566*4882a593Smuzhiyun .vidioc_querybuf = vb2_ioctl_querybuf,
1567*4882a593Smuzhiyun .vidioc_qbuf = vb2_ioctl_qbuf,
1568*4882a593Smuzhiyun .vidioc_expbuf = vb2_ioctl_expbuf,
1569*4882a593Smuzhiyun .vidioc_dqbuf = vb2_ioctl_dqbuf,
1570*4882a593Smuzhiyun .vidioc_create_bufs = vb2_ioctl_create_bufs,
1571*4882a593Smuzhiyun .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1572*4882a593Smuzhiyun .vidioc_streamon = vb2_ioctl_streamon,
1573*4882a593Smuzhiyun .vidioc_streamoff = vb2_ioctl_streamoff,
1574*4882a593Smuzhiyun
1575*4882a593Smuzhiyun .vidioc_g_parm = isc_g_parm,
1576*4882a593Smuzhiyun .vidioc_s_parm = isc_s_parm,
1577*4882a593Smuzhiyun .vidioc_enum_framesizes = isc_enum_framesizes,
1578*4882a593Smuzhiyun .vidioc_enum_frameintervals = isc_enum_frameintervals,
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun .vidioc_log_status = v4l2_ctrl_log_status,
1581*4882a593Smuzhiyun .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1582*4882a593Smuzhiyun .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1583*4882a593Smuzhiyun };
1584*4882a593Smuzhiyun
isc_open(struct file * file)1585*4882a593Smuzhiyun static int isc_open(struct file *file)
1586*4882a593Smuzhiyun {
1587*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1588*4882a593Smuzhiyun struct v4l2_subdev *sd = isc->current_subdev->sd;
1589*4882a593Smuzhiyun int ret;
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun if (mutex_lock_interruptible(&isc->lock))
1592*4882a593Smuzhiyun return -ERESTARTSYS;
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun ret = v4l2_fh_open(file);
1595*4882a593Smuzhiyun if (ret < 0)
1596*4882a593Smuzhiyun goto unlock;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun if (!v4l2_fh_is_singular_file(file))
1599*4882a593Smuzhiyun goto unlock;
1600*4882a593Smuzhiyun
1601*4882a593Smuzhiyun ret = v4l2_subdev_call(sd, core, s_power, 1);
1602*4882a593Smuzhiyun if (ret < 0 && ret != -ENOIOCTLCMD) {
1603*4882a593Smuzhiyun v4l2_fh_release(file);
1604*4882a593Smuzhiyun goto unlock;
1605*4882a593Smuzhiyun }
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun ret = isc_set_fmt(isc, &isc->fmt);
1608*4882a593Smuzhiyun if (ret) {
1609*4882a593Smuzhiyun v4l2_subdev_call(sd, core, s_power, 0);
1610*4882a593Smuzhiyun v4l2_fh_release(file);
1611*4882a593Smuzhiyun }
1612*4882a593Smuzhiyun
1613*4882a593Smuzhiyun unlock:
1614*4882a593Smuzhiyun mutex_unlock(&isc->lock);
1615*4882a593Smuzhiyun return ret;
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
isc_release(struct file * file)1618*4882a593Smuzhiyun static int isc_release(struct file *file)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun struct isc_device *isc = video_drvdata(file);
1621*4882a593Smuzhiyun struct v4l2_subdev *sd = isc->current_subdev->sd;
1622*4882a593Smuzhiyun bool fh_singular;
1623*4882a593Smuzhiyun int ret;
1624*4882a593Smuzhiyun
1625*4882a593Smuzhiyun mutex_lock(&isc->lock);
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun fh_singular = v4l2_fh_is_singular_file(file);
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun ret = _vb2_fop_release(file, NULL);
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun if (fh_singular)
1632*4882a593Smuzhiyun v4l2_subdev_call(sd, core, s_power, 0);
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun mutex_unlock(&isc->lock);
1635*4882a593Smuzhiyun
1636*4882a593Smuzhiyun return ret;
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun static const struct v4l2_file_operations isc_fops = {
1640*4882a593Smuzhiyun .owner = THIS_MODULE,
1641*4882a593Smuzhiyun .open = isc_open,
1642*4882a593Smuzhiyun .release = isc_release,
1643*4882a593Smuzhiyun .unlocked_ioctl = video_ioctl2,
1644*4882a593Smuzhiyun .read = vb2_fop_read,
1645*4882a593Smuzhiyun .mmap = vb2_fop_mmap,
1646*4882a593Smuzhiyun .poll = vb2_fop_poll,
1647*4882a593Smuzhiyun };
1648*4882a593Smuzhiyun
isc_interrupt(int irq,void * dev_id)1649*4882a593Smuzhiyun irqreturn_t isc_interrupt(int irq, void *dev_id)
1650*4882a593Smuzhiyun {
1651*4882a593Smuzhiyun struct isc_device *isc = (struct isc_device *)dev_id;
1652*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
1653*4882a593Smuzhiyun u32 isc_intsr, isc_intmask, pending;
1654*4882a593Smuzhiyun irqreturn_t ret = IRQ_NONE;
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun regmap_read(regmap, ISC_INTSR, &isc_intsr);
1657*4882a593Smuzhiyun regmap_read(regmap, ISC_INTMASK, &isc_intmask);
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun pending = isc_intsr & isc_intmask;
1660*4882a593Smuzhiyun
1661*4882a593Smuzhiyun if (likely(pending & ISC_INT_DDONE)) {
1662*4882a593Smuzhiyun spin_lock(&isc->dma_queue_lock);
1663*4882a593Smuzhiyun if (isc->cur_frm) {
1664*4882a593Smuzhiyun struct vb2_v4l2_buffer *vbuf = &isc->cur_frm->vb;
1665*4882a593Smuzhiyun struct vb2_buffer *vb = &vbuf->vb2_buf;
1666*4882a593Smuzhiyun
1667*4882a593Smuzhiyun vb->timestamp = ktime_get_ns();
1668*4882a593Smuzhiyun vbuf->sequence = isc->sequence++;
1669*4882a593Smuzhiyun vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1670*4882a593Smuzhiyun isc->cur_frm = NULL;
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun if (!list_empty(&isc->dma_queue) && !isc->stop) {
1674*4882a593Smuzhiyun isc->cur_frm = list_first_entry(&isc->dma_queue,
1675*4882a593Smuzhiyun struct isc_buffer, list);
1676*4882a593Smuzhiyun list_del(&isc->cur_frm->list);
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun isc_start_dma(isc);
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun if (isc->stop)
1682*4882a593Smuzhiyun complete(&isc->comp);
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun ret = IRQ_HANDLED;
1685*4882a593Smuzhiyun spin_unlock(&isc->dma_queue_lock);
1686*4882a593Smuzhiyun }
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun if (pending & ISC_INT_HISDONE) {
1689*4882a593Smuzhiyun schedule_work(&isc->awb_work);
1690*4882a593Smuzhiyun ret = IRQ_HANDLED;
1691*4882a593Smuzhiyun }
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun return ret;
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun
isc_hist_count(struct isc_device * isc,u32 * min,u32 * max)1696*4882a593Smuzhiyun static void isc_hist_count(struct isc_device *isc, u32 *min, u32 *max)
1697*4882a593Smuzhiyun {
1698*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
1699*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
1700*4882a593Smuzhiyun u32 *hist_count = &ctrls->hist_count[ctrls->hist_id];
1701*4882a593Smuzhiyun u32 *hist_entry = &ctrls->hist_entry[0];
1702*4882a593Smuzhiyun u32 i;
1703*4882a593Smuzhiyun
1704*4882a593Smuzhiyun *min = 0;
1705*4882a593Smuzhiyun *max = HIST_ENTRIES;
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun regmap_bulk_read(regmap, ISC_HIS_ENTRY, hist_entry, HIST_ENTRIES);
1708*4882a593Smuzhiyun
1709*4882a593Smuzhiyun *hist_count = 0;
1710*4882a593Smuzhiyun /*
1711*4882a593Smuzhiyun * we deliberately ignore the end of the histogram,
1712*4882a593Smuzhiyun * the most white pixels
1713*4882a593Smuzhiyun */
1714*4882a593Smuzhiyun for (i = 1; i < HIST_ENTRIES; i++) {
1715*4882a593Smuzhiyun if (*hist_entry && !*min)
1716*4882a593Smuzhiyun *min = i;
1717*4882a593Smuzhiyun if (*hist_entry)
1718*4882a593Smuzhiyun *max = i;
1719*4882a593Smuzhiyun *hist_count += i * (*hist_entry++);
1720*4882a593Smuzhiyun }
1721*4882a593Smuzhiyun
1722*4882a593Smuzhiyun if (!*min)
1723*4882a593Smuzhiyun *min = 1;
1724*4882a593Smuzhiyun }
1725*4882a593Smuzhiyun
isc_wb_update(struct isc_ctrls * ctrls)1726*4882a593Smuzhiyun static void isc_wb_update(struct isc_ctrls *ctrls)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun u32 *hist_count = &ctrls->hist_count[0];
1729*4882a593Smuzhiyun u32 c, offset[4];
1730*4882a593Smuzhiyun u64 avg = 0;
1731*4882a593Smuzhiyun /* We compute two gains, stretch gain and grey world gain */
1732*4882a593Smuzhiyun u32 s_gain[4], gw_gain[4];
1733*4882a593Smuzhiyun
1734*4882a593Smuzhiyun /*
1735*4882a593Smuzhiyun * According to Grey World, we need to set gains for R/B to normalize
1736*4882a593Smuzhiyun * them towards the green channel.
1737*4882a593Smuzhiyun * Thus we want to keep Green as fixed and adjust only Red/Blue
1738*4882a593Smuzhiyun * Compute the average of the both green channels first
1739*4882a593Smuzhiyun */
1740*4882a593Smuzhiyun avg = (u64)hist_count[ISC_HIS_CFG_MODE_GR] +
1741*4882a593Smuzhiyun (u64)hist_count[ISC_HIS_CFG_MODE_GB];
1742*4882a593Smuzhiyun avg >>= 1;
1743*4882a593Smuzhiyun
1744*4882a593Smuzhiyun /* Green histogram is null, nothing to do */
1745*4882a593Smuzhiyun if (!avg)
1746*4882a593Smuzhiyun return;
1747*4882a593Smuzhiyun
1748*4882a593Smuzhiyun for (c = ISC_HIS_CFG_MODE_GR; c <= ISC_HIS_CFG_MODE_B; c++) {
1749*4882a593Smuzhiyun /*
1750*4882a593Smuzhiyun * the color offset is the minimum value of the histogram.
1751*4882a593Smuzhiyun * we stretch this color to the full range by substracting
1752*4882a593Smuzhiyun * this value from the color component.
1753*4882a593Smuzhiyun */
1754*4882a593Smuzhiyun offset[c] = ctrls->hist_minmax[c][HIST_MIN_INDEX];
1755*4882a593Smuzhiyun /*
1756*4882a593Smuzhiyun * The offset is always at least 1. If the offset is 1, we do
1757*4882a593Smuzhiyun * not need to adjust it, so our result must be zero.
1758*4882a593Smuzhiyun * the offset is computed in a histogram on 9 bits (0..512)
1759*4882a593Smuzhiyun * but the offset in register is based on
1760*4882a593Smuzhiyun * 12 bits pipeline (0..4096).
1761*4882a593Smuzhiyun * we need to shift with the 3 bits that the histogram is
1762*4882a593Smuzhiyun * ignoring
1763*4882a593Smuzhiyun */
1764*4882a593Smuzhiyun ctrls->offset[c] = (offset[c] - 1) << 3;
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun /*
1767*4882a593Smuzhiyun * the offset is then taken and converted to 2's complements,
1768*4882a593Smuzhiyun * and must be negative, as we subtract this value from the
1769*4882a593Smuzhiyun * color components
1770*4882a593Smuzhiyun */
1771*4882a593Smuzhiyun ctrls->offset[c] = -ctrls->offset[c];
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun /*
1774*4882a593Smuzhiyun * the stretch gain is the total number of histogram bins
1775*4882a593Smuzhiyun * divided by the actual range of color component (Max - Min)
1776*4882a593Smuzhiyun * If we compute gain like this, the actual color component
1777*4882a593Smuzhiyun * will be stretched to the full histogram.
1778*4882a593Smuzhiyun * We need to shift 9 bits for precision, we have 9 bits for
1779*4882a593Smuzhiyun * decimals
1780*4882a593Smuzhiyun */
1781*4882a593Smuzhiyun s_gain[c] = (HIST_ENTRIES << 9) /
1782*4882a593Smuzhiyun (ctrls->hist_minmax[c][HIST_MAX_INDEX] -
1783*4882a593Smuzhiyun ctrls->hist_minmax[c][HIST_MIN_INDEX] + 1);
1784*4882a593Smuzhiyun
1785*4882a593Smuzhiyun /*
1786*4882a593Smuzhiyun * Now we have to compute the gain w.r.t. the average.
1787*4882a593Smuzhiyun * Add/lose gain to the component towards the average.
1788*4882a593Smuzhiyun * If it happens that the component is zero, use the
1789*4882a593Smuzhiyun * fixed point value : 1.0 gain.
1790*4882a593Smuzhiyun */
1791*4882a593Smuzhiyun if (hist_count[c])
1792*4882a593Smuzhiyun gw_gain[c] = div_u64(avg << 9, hist_count[c]);
1793*4882a593Smuzhiyun else
1794*4882a593Smuzhiyun gw_gain[c] = 1 << 9;
1795*4882a593Smuzhiyun
1796*4882a593Smuzhiyun /* multiply both gains and adjust for decimals */
1797*4882a593Smuzhiyun ctrls->gain[c] = s_gain[c] * gw_gain[c];
1798*4882a593Smuzhiyun ctrls->gain[c] >>= 9;
1799*4882a593Smuzhiyun }
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun
isc_awb_work(struct work_struct * w)1802*4882a593Smuzhiyun static void isc_awb_work(struct work_struct *w)
1803*4882a593Smuzhiyun {
1804*4882a593Smuzhiyun struct isc_device *isc =
1805*4882a593Smuzhiyun container_of(w, struct isc_device, awb_work);
1806*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
1807*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
1808*4882a593Smuzhiyun u32 hist_id = ctrls->hist_id;
1809*4882a593Smuzhiyun u32 baysel;
1810*4882a593Smuzhiyun unsigned long flags;
1811*4882a593Smuzhiyun u32 min, max;
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun /* streaming is not active anymore */
1814*4882a593Smuzhiyun if (isc->stop)
1815*4882a593Smuzhiyun return;
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun if (ctrls->hist_stat != HIST_ENABLED)
1818*4882a593Smuzhiyun return;
1819*4882a593Smuzhiyun
1820*4882a593Smuzhiyun isc_hist_count(isc, &min, &max);
1821*4882a593Smuzhiyun ctrls->hist_minmax[hist_id][HIST_MIN_INDEX] = min;
1822*4882a593Smuzhiyun ctrls->hist_minmax[hist_id][HIST_MAX_INDEX] = max;
1823*4882a593Smuzhiyun
1824*4882a593Smuzhiyun if (hist_id != ISC_HIS_CFG_MODE_B) {
1825*4882a593Smuzhiyun hist_id++;
1826*4882a593Smuzhiyun } else {
1827*4882a593Smuzhiyun isc_wb_update(ctrls);
1828*4882a593Smuzhiyun hist_id = ISC_HIS_CFG_MODE_GR;
1829*4882a593Smuzhiyun }
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun ctrls->hist_id = hist_id;
1832*4882a593Smuzhiyun baysel = isc->config.sd_format->cfa_baycfg << ISC_HIS_CFG_BAYSEL_SHIFT;
1833*4882a593Smuzhiyun
1834*4882a593Smuzhiyun pm_runtime_get_sync(isc->dev);
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun /*
1837*4882a593Smuzhiyun * only update if we have all the required histograms and controls
1838*4882a593Smuzhiyun * if awb has been disabled, we need to reset registers as well.
1839*4882a593Smuzhiyun */
1840*4882a593Smuzhiyun if (hist_id == ISC_HIS_CFG_MODE_GR || ctrls->awb == ISC_WB_NONE) {
1841*4882a593Smuzhiyun /*
1842*4882a593Smuzhiyun * It may happen that DMA Done IRQ will trigger while we are
1843*4882a593Smuzhiyun * updating white balance registers here.
1844*4882a593Smuzhiyun * In that case, only parts of the controls have been updated.
1845*4882a593Smuzhiyun * We can avoid that by locking the section.
1846*4882a593Smuzhiyun */
1847*4882a593Smuzhiyun spin_lock_irqsave(&isc->awb_lock, flags);
1848*4882a593Smuzhiyun isc_update_awb_ctrls(isc);
1849*4882a593Smuzhiyun spin_unlock_irqrestore(&isc->awb_lock, flags);
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun /*
1852*4882a593Smuzhiyun * if we are doing just the one time white balance adjustment,
1853*4882a593Smuzhiyun * we are basically done.
1854*4882a593Smuzhiyun */
1855*4882a593Smuzhiyun if (ctrls->awb == ISC_WB_ONETIME) {
1856*4882a593Smuzhiyun v4l2_info(&isc->v4l2_dev,
1857*4882a593Smuzhiyun "Completed one time white-balance adjustment.\n");
1858*4882a593Smuzhiyun /* update the v4l2 controls values */
1859*4882a593Smuzhiyun isc_update_v4l2_ctrls(isc);
1860*4882a593Smuzhiyun ctrls->awb = ISC_WB_NONE;
1861*4882a593Smuzhiyun }
1862*4882a593Smuzhiyun }
1863*4882a593Smuzhiyun regmap_write(regmap, ISC_HIS_CFG, hist_id | baysel | ISC_HIS_CFG_RAR);
1864*4882a593Smuzhiyun isc_update_profile(isc);
1865*4882a593Smuzhiyun /* if awb has been disabled, we don't need to start another histogram */
1866*4882a593Smuzhiyun if (ctrls->awb)
1867*4882a593Smuzhiyun regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ);
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun pm_runtime_put_sync(isc->dev);
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun
isc_s_ctrl(struct v4l2_ctrl * ctrl)1872*4882a593Smuzhiyun static int isc_s_ctrl(struct v4l2_ctrl *ctrl)
1873*4882a593Smuzhiyun {
1874*4882a593Smuzhiyun struct isc_device *isc = container_of(ctrl->handler,
1875*4882a593Smuzhiyun struct isc_device, ctrls.handler);
1876*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1879*4882a593Smuzhiyun return 0;
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun switch (ctrl->id) {
1882*4882a593Smuzhiyun case V4L2_CID_BRIGHTNESS:
1883*4882a593Smuzhiyun ctrls->brightness = ctrl->val & ISC_CBC_BRIGHT_MASK;
1884*4882a593Smuzhiyun break;
1885*4882a593Smuzhiyun case V4L2_CID_CONTRAST:
1886*4882a593Smuzhiyun ctrls->contrast = ctrl->val & ISC_CBC_CONTRAST_MASK;
1887*4882a593Smuzhiyun break;
1888*4882a593Smuzhiyun case V4L2_CID_GAMMA:
1889*4882a593Smuzhiyun ctrls->gamma_index = ctrl->val;
1890*4882a593Smuzhiyun break;
1891*4882a593Smuzhiyun default:
1892*4882a593Smuzhiyun return -EINVAL;
1893*4882a593Smuzhiyun }
1894*4882a593Smuzhiyun
1895*4882a593Smuzhiyun return 0;
1896*4882a593Smuzhiyun }
1897*4882a593Smuzhiyun
1898*4882a593Smuzhiyun static const struct v4l2_ctrl_ops isc_ctrl_ops = {
1899*4882a593Smuzhiyun .s_ctrl = isc_s_ctrl,
1900*4882a593Smuzhiyun };
1901*4882a593Smuzhiyun
isc_s_awb_ctrl(struct v4l2_ctrl * ctrl)1902*4882a593Smuzhiyun static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl)
1903*4882a593Smuzhiyun {
1904*4882a593Smuzhiyun struct isc_device *isc = container_of(ctrl->handler,
1905*4882a593Smuzhiyun struct isc_device, ctrls.handler);
1906*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
1907*4882a593Smuzhiyun
1908*4882a593Smuzhiyun if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
1909*4882a593Smuzhiyun return 0;
1910*4882a593Smuzhiyun
1911*4882a593Smuzhiyun switch (ctrl->id) {
1912*4882a593Smuzhiyun case V4L2_CID_AUTO_WHITE_BALANCE:
1913*4882a593Smuzhiyun if (ctrl->val == 1)
1914*4882a593Smuzhiyun ctrls->awb = ISC_WB_AUTO;
1915*4882a593Smuzhiyun else
1916*4882a593Smuzhiyun ctrls->awb = ISC_WB_NONE;
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun /* we did not configure ISC yet */
1919*4882a593Smuzhiyun if (!isc->config.sd_format)
1920*4882a593Smuzhiyun break;
1921*4882a593Smuzhiyun
1922*4882a593Smuzhiyun /* configure the controls with new values from v4l2 */
1923*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_R_GAIN]->is_new)
1924*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_R] = isc->r_gain_ctrl->val;
1925*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_B_GAIN]->is_new)
1926*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_B] = isc->b_gain_ctrl->val;
1927*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_GR_GAIN]->is_new)
1928*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_GR] = isc->gr_gain_ctrl->val;
1929*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_GB_GAIN]->is_new)
1930*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_GB] = isc->gb_gain_ctrl->val;
1931*4882a593Smuzhiyun
1932*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_R_OFF]->is_new)
1933*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_R] = isc->r_off_ctrl->val;
1934*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_B_OFF]->is_new)
1935*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_B] = isc->b_off_ctrl->val;
1936*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_GR_OFF]->is_new)
1937*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_GR] = isc->gr_off_ctrl->val;
1938*4882a593Smuzhiyun if (ctrl->cluster[ISC_CTRL_GB_OFF]->is_new)
1939*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_GB] = isc->gb_off_ctrl->val;
1940*4882a593Smuzhiyun
1941*4882a593Smuzhiyun isc_update_awb_ctrls(isc);
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun if (vb2_is_streaming(&isc->vb2_vidq)) {
1944*4882a593Smuzhiyun /*
1945*4882a593Smuzhiyun * If we are streaming, we can update profile to
1946*4882a593Smuzhiyun * have the new settings in place.
1947*4882a593Smuzhiyun */
1948*4882a593Smuzhiyun isc_update_profile(isc);
1949*4882a593Smuzhiyun } else {
1950*4882a593Smuzhiyun /*
1951*4882a593Smuzhiyun * The auto cluster will activate automatically this
1952*4882a593Smuzhiyun * control. This has to be deactivated when not
1953*4882a593Smuzhiyun * streaming.
1954*4882a593Smuzhiyun */
1955*4882a593Smuzhiyun v4l2_ctrl_activate(isc->do_wb_ctrl, false);
1956*4882a593Smuzhiyun }
1957*4882a593Smuzhiyun
1958*4882a593Smuzhiyun /* if we have autowhitebalance on, start histogram procedure */
1959*4882a593Smuzhiyun if (ctrls->awb == ISC_WB_AUTO &&
1960*4882a593Smuzhiyun vb2_is_streaming(&isc->vb2_vidq) &&
1961*4882a593Smuzhiyun ISC_IS_FORMAT_RAW(isc->config.sd_format->mbus_code))
1962*4882a593Smuzhiyun isc_set_histogram(isc, true);
1963*4882a593Smuzhiyun
1964*4882a593Smuzhiyun /*
1965*4882a593Smuzhiyun * for one time whitebalance adjustment, check the button,
1966*4882a593Smuzhiyun * if it's pressed, perform the one time operation.
1967*4882a593Smuzhiyun */
1968*4882a593Smuzhiyun if (ctrls->awb == ISC_WB_NONE &&
1969*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_DO_WB]->is_new &&
1970*4882a593Smuzhiyun !(ctrl->cluster[ISC_CTRL_DO_WB]->flags &
1971*4882a593Smuzhiyun V4L2_CTRL_FLAG_INACTIVE)) {
1972*4882a593Smuzhiyun ctrls->awb = ISC_WB_ONETIME;
1973*4882a593Smuzhiyun isc_set_histogram(isc, true);
1974*4882a593Smuzhiyun v4l2_dbg(1, debug, &isc->v4l2_dev,
1975*4882a593Smuzhiyun "One time white-balance started.\n");
1976*4882a593Smuzhiyun }
1977*4882a593Smuzhiyun return 0;
1978*4882a593Smuzhiyun }
1979*4882a593Smuzhiyun return 0;
1980*4882a593Smuzhiyun }
1981*4882a593Smuzhiyun
isc_g_volatile_awb_ctrl(struct v4l2_ctrl * ctrl)1982*4882a593Smuzhiyun static int isc_g_volatile_awb_ctrl(struct v4l2_ctrl *ctrl)
1983*4882a593Smuzhiyun {
1984*4882a593Smuzhiyun struct isc_device *isc = container_of(ctrl->handler,
1985*4882a593Smuzhiyun struct isc_device, ctrls.handler);
1986*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
1987*4882a593Smuzhiyun
1988*4882a593Smuzhiyun switch (ctrl->id) {
1989*4882a593Smuzhiyun /* being a cluster, this id will be called for every control */
1990*4882a593Smuzhiyun case V4L2_CID_AUTO_WHITE_BALANCE:
1991*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_R_GAIN]->val =
1992*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_R];
1993*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_B_GAIN]->val =
1994*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_B];
1995*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_GR_GAIN]->val =
1996*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_GR];
1997*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_GB_GAIN]->val =
1998*4882a593Smuzhiyun ctrls->gain[ISC_HIS_CFG_MODE_GB];
1999*4882a593Smuzhiyun
2000*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_R_OFF]->val =
2001*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_R];
2002*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_B_OFF]->val =
2003*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_B];
2004*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_GR_OFF]->val =
2005*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_GR];
2006*4882a593Smuzhiyun ctrl->cluster[ISC_CTRL_GB_OFF]->val =
2007*4882a593Smuzhiyun ctrls->offset[ISC_HIS_CFG_MODE_GB];
2008*4882a593Smuzhiyun break;
2009*4882a593Smuzhiyun }
2010*4882a593Smuzhiyun return 0;
2011*4882a593Smuzhiyun }
2012*4882a593Smuzhiyun
2013*4882a593Smuzhiyun static const struct v4l2_ctrl_ops isc_awb_ops = {
2014*4882a593Smuzhiyun .s_ctrl = isc_s_awb_ctrl,
2015*4882a593Smuzhiyun .g_volatile_ctrl = isc_g_volatile_awb_ctrl,
2016*4882a593Smuzhiyun };
2017*4882a593Smuzhiyun
2018*4882a593Smuzhiyun #define ISC_CTRL_OFF(_name, _id, _name_str) \
2019*4882a593Smuzhiyun static const struct v4l2_ctrl_config _name = { \
2020*4882a593Smuzhiyun .ops = &isc_awb_ops, \
2021*4882a593Smuzhiyun .id = _id, \
2022*4882a593Smuzhiyun .name = _name_str, \
2023*4882a593Smuzhiyun .type = V4L2_CTRL_TYPE_INTEGER, \
2024*4882a593Smuzhiyun .flags = V4L2_CTRL_FLAG_SLIDER, \
2025*4882a593Smuzhiyun .min = -4095, \
2026*4882a593Smuzhiyun .max = 4095, \
2027*4882a593Smuzhiyun .step = 1, \
2028*4882a593Smuzhiyun .def = 0, \
2029*4882a593Smuzhiyun }
2030*4882a593Smuzhiyun
2031*4882a593Smuzhiyun ISC_CTRL_OFF(isc_r_off_ctrl, ISC_CID_R_OFFSET, "Red Component Offset");
2032*4882a593Smuzhiyun ISC_CTRL_OFF(isc_b_off_ctrl, ISC_CID_B_OFFSET, "Blue Component Offset");
2033*4882a593Smuzhiyun ISC_CTRL_OFF(isc_gr_off_ctrl, ISC_CID_GR_OFFSET, "Green Red Component Offset");
2034*4882a593Smuzhiyun ISC_CTRL_OFF(isc_gb_off_ctrl, ISC_CID_GB_OFFSET, "Green Blue Component Offset");
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun #define ISC_CTRL_GAIN(_name, _id, _name_str) \
2037*4882a593Smuzhiyun static const struct v4l2_ctrl_config _name = { \
2038*4882a593Smuzhiyun .ops = &isc_awb_ops, \
2039*4882a593Smuzhiyun .id = _id, \
2040*4882a593Smuzhiyun .name = _name_str, \
2041*4882a593Smuzhiyun .type = V4L2_CTRL_TYPE_INTEGER, \
2042*4882a593Smuzhiyun .flags = V4L2_CTRL_FLAG_SLIDER, \
2043*4882a593Smuzhiyun .min = 0, \
2044*4882a593Smuzhiyun .max = 8191, \
2045*4882a593Smuzhiyun .step = 1, \
2046*4882a593Smuzhiyun .def = 512, \
2047*4882a593Smuzhiyun }
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun ISC_CTRL_GAIN(isc_r_gain_ctrl, ISC_CID_R_GAIN, "Red Component Gain");
2050*4882a593Smuzhiyun ISC_CTRL_GAIN(isc_b_gain_ctrl, ISC_CID_B_GAIN, "Blue Component Gain");
2051*4882a593Smuzhiyun ISC_CTRL_GAIN(isc_gr_gain_ctrl, ISC_CID_GR_GAIN, "Green Red Component Gain");
2052*4882a593Smuzhiyun ISC_CTRL_GAIN(isc_gb_gain_ctrl, ISC_CID_GB_GAIN, "Green Blue Component Gain");
2053*4882a593Smuzhiyun
isc_ctrl_init(struct isc_device * isc)2054*4882a593Smuzhiyun static int isc_ctrl_init(struct isc_device *isc)
2055*4882a593Smuzhiyun {
2056*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops = &isc_ctrl_ops;
2057*4882a593Smuzhiyun struct isc_ctrls *ctrls = &isc->ctrls;
2058*4882a593Smuzhiyun struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2059*4882a593Smuzhiyun int ret;
2060*4882a593Smuzhiyun
2061*4882a593Smuzhiyun ctrls->hist_stat = HIST_INIT;
2062*4882a593Smuzhiyun isc_reset_awb_ctrls(isc);
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun ret = v4l2_ctrl_handler_init(hdl, 13);
2065*4882a593Smuzhiyun if (ret < 0)
2066*4882a593Smuzhiyun return ret;
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun ctrls->brightness = 0;
2069*4882a593Smuzhiyun ctrls->contrast = 256;
2070*4882a593Smuzhiyun
2071*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -1024, 1023, 1, 0);
2072*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -2048, 2047, 1, 256);
2073*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAMMA, 0, GAMMA_MAX, 1, 2);
2074*4882a593Smuzhiyun isc->awb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
2075*4882a593Smuzhiyun V4L2_CID_AUTO_WHITE_BALANCE,
2076*4882a593Smuzhiyun 0, 1, 1, 1);
2077*4882a593Smuzhiyun
2078*4882a593Smuzhiyun /* do_white_balance is a button, so min,max,step,default are ignored */
2079*4882a593Smuzhiyun isc->do_wb_ctrl = v4l2_ctrl_new_std(hdl, &isc_awb_ops,
2080*4882a593Smuzhiyun V4L2_CID_DO_WHITE_BALANCE,
2081*4882a593Smuzhiyun 0, 0, 0, 0);
2082*4882a593Smuzhiyun
2083*4882a593Smuzhiyun if (!isc->do_wb_ctrl) {
2084*4882a593Smuzhiyun ret = hdl->error;
2085*4882a593Smuzhiyun v4l2_ctrl_handler_free(hdl);
2086*4882a593Smuzhiyun return ret;
2087*4882a593Smuzhiyun }
2088*4882a593Smuzhiyun
2089*4882a593Smuzhiyun v4l2_ctrl_activate(isc->do_wb_ctrl, false);
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun isc->r_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_gain_ctrl, NULL);
2092*4882a593Smuzhiyun isc->b_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_gain_ctrl, NULL);
2093*4882a593Smuzhiyun isc->gr_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_gain_ctrl, NULL);
2094*4882a593Smuzhiyun isc->gb_gain_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_gain_ctrl, NULL);
2095*4882a593Smuzhiyun isc->r_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_r_off_ctrl, NULL);
2096*4882a593Smuzhiyun isc->b_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_b_off_ctrl, NULL);
2097*4882a593Smuzhiyun isc->gr_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gr_off_ctrl, NULL);
2098*4882a593Smuzhiyun isc->gb_off_ctrl = v4l2_ctrl_new_custom(hdl, &isc_gb_off_ctrl, NULL);
2099*4882a593Smuzhiyun
2100*4882a593Smuzhiyun /*
2101*4882a593Smuzhiyun * The cluster is in auto mode with autowhitebalance enabled
2102*4882a593Smuzhiyun * and manual mode otherwise.
2103*4882a593Smuzhiyun */
2104*4882a593Smuzhiyun v4l2_ctrl_auto_cluster(10, &isc->awb_ctrl, 0, true);
2105*4882a593Smuzhiyun
2106*4882a593Smuzhiyun v4l2_ctrl_handler_setup(hdl);
2107*4882a593Smuzhiyun
2108*4882a593Smuzhiyun return 0;
2109*4882a593Smuzhiyun }
2110*4882a593Smuzhiyun
isc_async_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)2111*4882a593Smuzhiyun static int isc_async_bound(struct v4l2_async_notifier *notifier,
2112*4882a593Smuzhiyun struct v4l2_subdev *subdev,
2113*4882a593Smuzhiyun struct v4l2_async_subdev *asd)
2114*4882a593Smuzhiyun {
2115*4882a593Smuzhiyun struct isc_device *isc = container_of(notifier->v4l2_dev,
2116*4882a593Smuzhiyun struct isc_device, v4l2_dev);
2117*4882a593Smuzhiyun struct isc_subdev_entity *subdev_entity =
2118*4882a593Smuzhiyun container_of(notifier, struct isc_subdev_entity, notifier);
2119*4882a593Smuzhiyun
2120*4882a593Smuzhiyun if (video_is_registered(&isc->video_dev)) {
2121*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "only supports one sub-device.\n");
2122*4882a593Smuzhiyun return -EBUSY;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun
2125*4882a593Smuzhiyun subdev_entity->sd = subdev;
2126*4882a593Smuzhiyun
2127*4882a593Smuzhiyun return 0;
2128*4882a593Smuzhiyun }
2129*4882a593Smuzhiyun
isc_async_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_subdev * asd)2130*4882a593Smuzhiyun static void isc_async_unbind(struct v4l2_async_notifier *notifier,
2131*4882a593Smuzhiyun struct v4l2_subdev *subdev,
2132*4882a593Smuzhiyun struct v4l2_async_subdev *asd)
2133*4882a593Smuzhiyun {
2134*4882a593Smuzhiyun struct isc_device *isc = container_of(notifier->v4l2_dev,
2135*4882a593Smuzhiyun struct isc_device, v4l2_dev);
2136*4882a593Smuzhiyun cancel_work_sync(&isc->awb_work);
2137*4882a593Smuzhiyun video_unregister_device(&isc->video_dev);
2138*4882a593Smuzhiyun v4l2_ctrl_handler_free(&isc->ctrls.handler);
2139*4882a593Smuzhiyun }
2140*4882a593Smuzhiyun
find_format_by_code(unsigned int code,int * index)2141*4882a593Smuzhiyun static struct isc_format *find_format_by_code(unsigned int code, int *index)
2142*4882a593Smuzhiyun {
2143*4882a593Smuzhiyun struct isc_format *fmt = &formats_list[0];
2144*4882a593Smuzhiyun unsigned int i;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(formats_list); i++) {
2147*4882a593Smuzhiyun if (fmt->mbus_code == code) {
2148*4882a593Smuzhiyun *index = i;
2149*4882a593Smuzhiyun return fmt;
2150*4882a593Smuzhiyun }
2151*4882a593Smuzhiyun
2152*4882a593Smuzhiyun fmt++;
2153*4882a593Smuzhiyun }
2154*4882a593Smuzhiyun
2155*4882a593Smuzhiyun return NULL;
2156*4882a593Smuzhiyun }
2157*4882a593Smuzhiyun
isc_formats_init(struct isc_device * isc)2158*4882a593Smuzhiyun static int isc_formats_init(struct isc_device *isc)
2159*4882a593Smuzhiyun {
2160*4882a593Smuzhiyun struct isc_format *fmt;
2161*4882a593Smuzhiyun struct v4l2_subdev *subdev = isc->current_subdev->sd;
2162*4882a593Smuzhiyun unsigned int num_fmts, i, j;
2163*4882a593Smuzhiyun u32 list_size = ARRAY_SIZE(formats_list);
2164*4882a593Smuzhiyun struct v4l2_subdev_mbus_code_enum mbus_code = {
2165*4882a593Smuzhiyun .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2166*4882a593Smuzhiyun };
2167*4882a593Smuzhiyun
2168*4882a593Smuzhiyun num_fmts = 0;
2169*4882a593Smuzhiyun while (!v4l2_subdev_call(subdev, pad, enum_mbus_code,
2170*4882a593Smuzhiyun NULL, &mbus_code)) {
2171*4882a593Smuzhiyun mbus_code.index++;
2172*4882a593Smuzhiyun
2173*4882a593Smuzhiyun fmt = find_format_by_code(mbus_code.code, &i);
2174*4882a593Smuzhiyun if (!fmt) {
2175*4882a593Smuzhiyun v4l2_warn(&isc->v4l2_dev, "Mbus code %x not supported\n",
2176*4882a593Smuzhiyun mbus_code.code);
2177*4882a593Smuzhiyun continue;
2178*4882a593Smuzhiyun }
2179*4882a593Smuzhiyun
2180*4882a593Smuzhiyun fmt->sd_support = true;
2181*4882a593Smuzhiyun num_fmts++;
2182*4882a593Smuzhiyun }
2183*4882a593Smuzhiyun
2184*4882a593Smuzhiyun if (!num_fmts)
2185*4882a593Smuzhiyun return -ENXIO;
2186*4882a593Smuzhiyun
2187*4882a593Smuzhiyun isc->num_user_formats = num_fmts;
2188*4882a593Smuzhiyun isc->user_formats = devm_kcalloc(isc->dev,
2189*4882a593Smuzhiyun num_fmts, sizeof(*isc->user_formats),
2190*4882a593Smuzhiyun GFP_KERNEL);
2191*4882a593Smuzhiyun if (!isc->user_formats)
2192*4882a593Smuzhiyun return -ENOMEM;
2193*4882a593Smuzhiyun
2194*4882a593Smuzhiyun fmt = &formats_list[0];
2195*4882a593Smuzhiyun for (i = 0, j = 0; i < list_size; i++) {
2196*4882a593Smuzhiyun if (fmt->sd_support)
2197*4882a593Smuzhiyun isc->user_formats[j++] = fmt;
2198*4882a593Smuzhiyun fmt++;
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun return 0;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun
isc_set_default_fmt(struct isc_device * isc)2204*4882a593Smuzhiyun static int isc_set_default_fmt(struct isc_device *isc)
2205*4882a593Smuzhiyun {
2206*4882a593Smuzhiyun struct v4l2_format f = {
2207*4882a593Smuzhiyun .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
2208*4882a593Smuzhiyun .fmt.pix = {
2209*4882a593Smuzhiyun .width = VGA_WIDTH,
2210*4882a593Smuzhiyun .height = VGA_HEIGHT,
2211*4882a593Smuzhiyun .field = V4L2_FIELD_NONE,
2212*4882a593Smuzhiyun .pixelformat = isc->user_formats[0]->fourcc,
2213*4882a593Smuzhiyun },
2214*4882a593Smuzhiyun };
2215*4882a593Smuzhiyun int ret;
2216*4882a593Smuzhiyun
2217*4882a593Smuzhiyun ret = isc_try_fmt(isc, &f, NULL);
2218*4882a593Smuzhiyun if (ret)
2219*4882a593Smuzhiyun return ret;
2220*4882a593Smuzhiyun
2221*4882a593Smuzhiyun isc->fmt = f;
2222*4882a593Smuzhiyun return 0;
2223*4882a593Smuzhiyun }
2224*4882a593Smuzhiyun
isc_async_complete(struct v4l2_async_notifier * notifier)2225*4882a593Smuzhiyun static int isc_async_complete(struct v4l2_async_notifier *notifier)
2226*4882a593Smuzhiyun {
2227*4882a593Smuzhiyun struct isc_device *isc = container_of(notifier->v4l2_dev,
2228*4882a593Smuzhiyun struct isc_device, v4l2_dev);
2229*4882a593Smuzhiyun struct video_device *vdev = &isc->video_dev;
2230*4882a593Smuzhiyun struct vb2_queue *q = &isc->vb2_vidq;
2231*4882a593Smuzhiyun int ret = 0;
2232*4882a593Smuzhiyun
2233*4882a593Smuzhiyun INIT_WORK(&isc->awb_work, isc_awb_work);
2234*4882a593Smuzhiyun
2235*4882a593Smuzhiyun ret = v4l2_device_register_subdev_nodes(&isc->v4l2_dev);
2236*4882a593Smuzhiyun if (ret < 0) {
2237*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "Failed to register subdev nodes\n");
2238*4882a593Smuzhiyun return ret;
2239*4882a593Smuzhiyun }
2240*4882a593Smuzhiyun
2241*4882a593Smuzhiyun isc->current_subdev = container_of(notifier,
2242*4882a593Smuzhiyun struct isc_subdev_entity, notifier);
2243*4882a593Smuzhiyun mutex_init(&isc->lock);
2244*4882a593Smuzhiyun init_completion(&isc->comp);
2245*4882a593Smuzhiyun
2246*4882a593Smuzhiyun /* Initialize videobuf2 queue */
2247*4882a593Smuzhiyun q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2248*4882a593Smuzhiyun q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2249*4882a593Smuzhiyun q->drv_priv = isc;
2250*4882a593Smuzhiyun q->buf_struct_size = sizeof(struct isc_buffer);
2251*4882a593Smuzhiyun q->ops = &isc_vb2_ops;
2252*4882a593Smuzhiyun q->mem_ops = &vb2_dma_contig_memops;
2253*4882a593Smuzhiyun q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2254*4882a593Smuzhiyun q->lock = &isc->lock;
2255*4882a593Smuzhiyun q->min_buffers_needed = 1;
2256*4882a593Smuzhiyun q->dev = isc->dev;
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun ret = vb2_queue_init(q);
2259*4882a593Smuzhiyun if (ret < 0) {
2260*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev,
2261*4882a593Smuzhiyun "vb2_queue_init() failed: %d\n", ret);
2262*4882a593Smuzhiyun goto isc_async_complete_err;
2263*4882a593Smuzhiyun }
2264*4882a593Smuzhiyun
2265*4882a593Smuzhiyun /* Init video dma queues */
2266*4882a593Smuzhiyun INIT_LIST_HEAD(&isc->dma_queue);
2267*4882a593Smuzhiyun spin_lock_init(&isc->dma_queue_lock);
2268*4882a593Smuzhiyun spin_lock_init(&isc->awb_lock);
2269*4882a593Smuzhiyun
2270*4882a593Smuzhiyun ret = isc_formats_init(isc);
2271*4882a593Smuzhiyun if (ret < 0) {
2272*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev,
2273*4882a593Smuzhiyun "Init format failed: %d\n", ret);
2274*4882a593Smuzhiyun goto isc_async_complete_err;
2275*4882a593Smuzhiyun }
2276*4882a593Smuzhiyun
2277*4882a593Smuzhiyun ret = isc_set_default_fmt(isc);
2278*4882a593Smuzhiyun if (ret) {
2279*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "Could not set default format\n");
2280*4882a593Smuzhiyun goto isc_async_complete_err;
2281*4882a593Smuzhiyun }
2282*4882a593Smuzhiyun
2283*4882a593Smuzhiyun ret = isc_ctrl_init(isc);
2284*4882a593Smuzhiyun if (ret) {
2285*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev, "Init isc ctrols failed: %d\n", ret);
2286*4882a593Smuzhiyun goto isc_async_complete_err;
2287*4882a593Smuzhiyun }
2288*4882a593Smuzhiyun
2289*4882a593Smuzhiyun /* Register video device */
2290*4882a593Smuzhiyun strscpy(vdev->name, ATMEL_ISC_NAME, sizeof(vdev->name));
2291*4882a593Smuzhiyun vdev->release = video_device_release_empty;
2292*4882a593Smuzhiyun vdev->fops = &isc_fops;
2293*4882a593Smuzhiyun vdev->ioctl_ops = &isc_ioctl_ops;
2294*4882a593Smuzhiyun vdev->v4l2_dev = &isc->v4l2_dev;
2295*4882a593Smuzhiyun vdev->vfl_dir = VFL_DIR_RX;
2296*4882a593Smuzhiyun vdev->queue = q;
2297*4882a593Smuzhiyun vdev->lock = &isc->lock;
2298*4882a593Smuzhiyun vdev->ctrl_handler = &isc->ctrls.handler;
2299*4882a593Smuzhiyun vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
2300*4882a593Smuzhiyun video_set_drvdata(vdev, isc);
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2303*4882a593Smuzhiyun if (ret < 0) {
2304*4882a593Smuzhiyun v4l2_err(&isc->v4l2_dev,
2305*4882a593Smuzhiyun "video_register_device failed: %d\n", ret);
2306*4882a593Smuzhiyun goto isc_async_complete_err;
2307*4882a593Smuzhiyun }
2308*4882a593Smuzhiyun
2309*4882a593Smuzhiyun return 0;
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun isc_async_complete_err:
2312*4882a593Smuzhiyun mutex_destroy(&isc->lock);
2313*4882a593Smuzhiyun return ret;
2314*4882a593Smuzhiyun }
2315*4882a593Smuzhiyun
2316*4882a593Smuzhiyun const struct v4l2_async_notifier_operations isc_async_ops = {
2317*4882a593Smuzhiyun .bound = isc_async_bound,
2318*4882a593Smuzhiyun .unbind = isc_async_unbind,
2319*4882a593Smuzhiyun .complete = isc_async_complete,
2320*4882a593Smuzhiyun };
2321*4882a593Smuzhiyun
isc_subdev_cleanup(struct isc_device * isc)2322*4882a593Smuzhiyun void isc_subdev_cleanup(struct isc_device *isc)
2323*4882a593Smuzhiyun {
2324*4882a593Smuzhiyun struct isc_subdev_entity *subdev_entity;
2325*4882a593Smuzhiyun
2326*4882a593Smuzhiyun list_for_each_entry(subdev_entity, &isc->subdev_entities, list) {
2327*4882a593Smuzhiyun v4l2_async_notifier_unregister(&subdev_entity->notifier);
2328*4882a593Smuzhiyun v4l2_async_notifier_cleanup(&subdev_entity->notifier);
2329*4882a593Smuzhiyun }
2330*4882a593Smuzhiyun
2331*4882a593Smuzhiyun INIT_LIST_HEAD(&isc->subdev_entities);
2332*4882a593Smuzhiyun }
2333*4882a593Smuzhiyun
isc_pipeline_init(struct isc_device * isc)2334*4882a593Smuzhiyun int isc_pipeline_init(struct isc_device *isc)
2335*4882a593Smuzhiyun {
2336*4882a593Smuzhiyun struct device *dev = isc->dev;
2337*4882a593Smuzhiyun struct regmap *regmap = isc->regmap;
2338*4882a593Smuzhiyun struct regmap_field *regs;
2339*4882a593Smuzhiyun unsigned int i;
2340*4882a593Smuzhiyun
2341*4882a593Smuzhiyun /* WB-->CFA-->CC-->GAM-->CSC-->CBC-->SUB422-->SUB420 */
2342*4882a593Smuzhiyun const struct reg_field regfields[ISC_PIPE_LINE_NODE_NUM] = {
2343*4882a593Smuzhiyun REG_FIELD(ISC_WB_CTRL, 0, 0),
2344*4882a593Smuzhiyun REG_FIELD(ISC_CFA_CTRL, 0, 0),
2345*4882a593Smuzhiyun REG_FIELD(ISC_CC_CTRL, 0, 0),
2346*4882a593Smuzhiyun REG_FIELD(ISC_GAM_CTRL, 0, 0),
2347*4882a593Smuzhiyun REG_FIELD(ISC_GAM_CTRL, 1, 1),
2348*4882a593Smuzhiyun REG_FIELD(ISC_GAM_CTRL, 2, 2),
2349*4882a593Smuzhiyun REG_FIELD(ISC_GAM_CTRL, 3, 3),
2350*4882a593Smuzhiyun REG_FIELD(ISC_CSC_CTRL, 0, 0),
2351*4882a593Smuzhiyun REG_FIELD(ISC_CBC_CTRL, 0, 0),
2352*4882a593Smuzhiyun REG_FIELD(ISC_SUB422_CTRL, 0, 0),
2353*4882a593Smuzhiyun REG_FIELD(ISC_SUB420_CTRL, 0, 0),
2354*4882a593Smuzhiyun };
2355*4882a593Smuzhiyun
2356*4882a593Smuzhiyun for (i = 0; i < ISC_PIPE_LINE_NODE_NUM; i++) {
2357*4882a593Smuzhiyun regs = devm_regmap_field_alloc(dev, regmap, regfields[i]);
2358*4882a593Smuzhiyun if (IS_ERR(regs))
2359*4882a593Smuzhiyun return PTR_ERR(regs);
2360*4882a593Smuzhiyun
2361*4882a593Smuzhiyun isc->pipeline[i] = regs;
2362*4882a593Smuzhiyun }
2363*4882a593Smuzhiyun
2364*4882a593Smuzhiyun return 0;
2365*4882a593Smuzhiyun }
2366*4882a593Smuzhiyun
2367*4882a593Smuzhiyun /* regmap configuration */
2368*4882a593Smuzhiyun #define ATMEL_ISC_REG_MAX 0xbfc
2369*4882a593Smuzhiyun const struct regmap_config isc_regmap_config = {
2370*4882a593Smuzhiyun .reg_bits = 32,
2371*4882a593Smuzhiyun .reg_stride = 4,
2372*4882a593Smuzhiyun .val_bits = 32,
2373*4882a593Smuzhiyun .max_register = ATMEL_ISC_REG_MAX,
2374*4882a593Smuzhiyun };
2375*4882a593Smuzhiyun
2376