xref: /openbmc/linux/drivers/media/i2c/rj54n1cb0c.c (revision 4b0aaacee51eb6592a03fdefd5ce97558518e291)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for RJ54N1CB0C CMOS Image Sensor from Sharp
4  *
5  * Copyright (C) 2018, Jacopo Mondi <jacopo@jmondi.org>
6  *
7  * Copyright (C) 2009, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/v4l2-mediabus.h>
17 #include <linux/videodev2.h>
18 
19 #include <media/i2c/rj54n1cb0c.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-subdev.h>
23 
24 #define RJ54N1_DEV_CODE			0x0400
25 #define RJ54N1_DEV_CODE2		0x0401
26 #define RJ54N1_OUT_SEL			0x0403
27 #define RJ54N1_XY_OUTPUT_SIZE_S_H	0x0404
28 #define RJ54N1_X_OUTPUT_SIZE_S_L	0x0405
29 #define RJ54N1_Y_OUTPUT_SIZE_S_L	0x0406
30 #define RJ54N1_XY_OUTPUT_SIZE_P_H	0x0407
31 #define RJ54N1_X_OUTPUT_SIZE_P_L	0x0408
32 #define RJ54N1_Y_OUTPUT_SIZE_P_L	0x0409
33 #define RJ54N1_LINE_LENGTH_PCK_S_H	0x040a
34 #define RJ54N1_LINE_LENGTH_PCK_S_L	0x040b
35 #define RJ54N1_LINE_LENGTH_PCK_P_H	0x040c
36 #define RJ54N1_LINE_LENGTH_PCK_P_L	0x040d
37 #define RJ54N1_RESIZE_N			0x040e
38 #define RJ54N1_RESIZE_N_STEP		0x040f
39 #define RJ54N1_RESIZE_STEP		0x0410
40 #define RJ54N1_RESIZE_HOLD_H		0x0411
41 #define RJ54N1_RESIZE_HOLD_L		0x0412
42 #define RJ54N1_H_OBEN_OFS		0x0413
43 #define RJ54N1_V_OBEN_OFS		0x0414
44 #define RJ54N1_RESIZE_CONTROL		0x0415
45 #define RJ54N1_STILL_CONTROL		0x0417
46 #define RJ54N1_INC_USE_SEL_H		0x0425
47 #define RJ54N1_INC_USE_SEL_L		0x0426
48 #define RJ54N1_MIRROR_STILL_MODE	0x0427
49 #define RJ54N1_INIT_START		0x0428
50 #define RJ54N1_SCALE_1_2_LEV		0x0429
51 #define RJ54N1_SCALE_4_LEV		0x042a
52 #define RJ54N1_Y_GAIN			0x04d8
53 #define RJ54N1_APT_GAIN_UP		0x04fa
54 #define RJ54N1_RA_SEL_UL		0x0530
55 #define RJ54N1_BYTE_SWAP		0x0531
56 #define RJ54N1_OUT_SIGPO		0x053b
57 #define RJ54N1_WB_SEL_WEIGHT_I		0x054e
58 #define RJ54N1_BIT8_WB			0x0569
59 #define RJ54N1_HCAPS_WB			0x056a
60 #define RJ54N1_VCAPS_WB			0x056b
61 #define RJ54N1_HCAPE_WB			0x056c
62 #define RJ54N1_VCAPE_WB			0x056d
63 #define RJ54N1_EXPOSURE_CONTROL		0x058c
64 #define RJ54N1_FRAME_LENGTH_S_H		0x0595
65 #define RJ54N1_FRAME_LENGTH_S_L		0x0596
66 #define RJ54N1_FRAME_LENGTH_P_H		0x0597
67 #define RJ54N1_FRAME_LENGTH_P_L		0x0598
68 #define RJ54N1_PEAK_H			0x05b7
69 #define RJ54N1_PEAK_50			0x05b8
70 #define RJ54N1_PEAK_60			0x05b9
71 #define RJ54N1_PEAK_DIFF		0x05ba
72 #define RJ54N1_IOC			0x05ef
73 #define RJ54N1_TG_BYPASS		0x0700
74 #define RJ54N1_PLL_L			0x0701
75 #define RJ54N1_PLL_N			0x0702
76 #define RJ54N1_PLL_EN			0x0704
77 #define RJ54N1_RATIO_TG			0x0706
78 #define RJ54N1_RATIO_T			0x0707
79 #define RJ54N1_RATIO_R			0x0708
80 #define RJ54N1_RAMP_TGCLK_EN		0x0709
81 #define RJ54N1_OCLK_DSP			0x0710
82 #define RJ54N1_RATIO_OP			0x0711
83 #define RJ54N1_RATIO_O			0x0712
84 #define RJ54N1_OCLK_SEL_EN		0x0713
85 #define RJ54N1_CLK_RST			0x0717
86 #define RJ54N1_RESET_STANDBY		0x0718
87 #define RJ54N1_FWFLG			0x07fe
88 
89 #define E_EXCLK				(1 << 7)
90 #define SOFT_STDBY			(1 << 4)
91 #define SEN_RSTX			(1 << 2)
92 #define TG_RSTX				(1 << 1)
93 #define DSP_RSTX			(1 << 0)
94 
95 #define RESIZE_HOLD_SEL			(1 << 2)
96 #define RESIZE_GO			(1 << 1)
97 
98 /*
99  * When cropping, the camera automatically centers the cropped region, there
100  * doesn't seem to be a way to specify an explicit location of the rectangle.
101  */
102 #define RJ54N1_COLUMN_SKIP		0
103 #define RJ54N1_ROW_SKIP			0
104 #define RJ54N1_MAX_WIDTH		1600
105 #define RJ54N1_MAX_HEIGHT		1200
106 
107 #define PLL_L				2
108 #define PLL_N				0x31
109 
110 /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
111 
112 /* RJ54N1CB0C has only one fixed colorspace per pixelcode */
113 struct rj54n1_datafmt {
114 	u32	code;
115 	enum v4l2_colorspace		colorspace;
116 };
117 
118 /* Find a data format by a pixel code in an array */
119 static const struct rj54n1_datafmt *rj54n1_find_datafmt(
120 	u32 code, const struct rj54n1_datafmt *fmt,
121 	int n)
122 {
123 	int i;
124 	for (i = 0; i < n; i++)
125 		if (fmt[i].code == code)
126 			return fmt + i;
127 
128 	return NULL;
129 }
130 
131 static const struct rj54n1_datafmt rj54n1_colour_fmts[] = {
132 	{MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
133 	{MEDIA_BUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
134 	{MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
135 	{MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB},
136 	{MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
137 	{MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE, V4L2_COLORSPACE_SRGB},
138 	{MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE, V4L2_COLORSPACE_SRGB},
139 	{MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE, V4L2_COLORSPACE_SRGB},
140 	{MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
141 };
142 
143 struct rj54n1_clock_div {
144 	u8 ratio_tg;	/* can be 0 or an odd number */
145 	u8 ratio_t;
146 	u8 ratio_r;
147 	u8 ratio_op;
148 	u8 ratio_o;
149 };
150 
151 struct rj54n1 {
152 	struct v4l2_subdev subdev;
153 	struct v4l2_ctrl_handler hdl;
154 	struct clk *clk;
155 	struct gpio_desc *pwup_gpio;
156 	struct gpio_desc *enable_gpio;
157 	struct rj54n1_clock_div clk_div;
158 	const struct rj54n1_datafmt *fmt;
159 	struct v4l2_rect rect;	/* Sensor window */
160 	unsigned int tgclk_mhz;
161 	bool auto_wb;
162 	unsigned short width;	/* Output window */
163 	unsigned short height;
164 	unsigned short resize;	/* Sensor * 1024 / resize = Output */
165 	unsigned short scale;
166 	u8 bank;
167 };
168 
169 struct rj54n1_reg_val {
170 	u16 reg;
171 	u8 val;
172 };
173 
174 static const struct rj54n1_reg_val bank_4[] = {
175 	{0x417, 0},
176 	{0x42c, 0},
177 	{0x42d, 0xf0},
178 	{0x42e, 0},
179 	{0x42f, 0x50},
180 	{0x430, 0xf5},
181 	{0x431, 0x16},
182 	{0x432, 0x20},
183 	{0x433, 0},
184 	{0x434, 0xc8},
185 	{0x43c, 8},
186 	{0x43e, 0x90},
187 	{0x445, 0x83},
188 	{0x4ba, 0x58},
189 	{0x4bb, 4},
190 	{0x4bc, 0x20},
191 	{0x4db, 4},
192 	{0x4fe, 2},
193 };
194 
195 static const struct rj54n1_reg_val bank_5[] = {
196 	{0x514, 0},
197 	{0x516, 0},
198 	{0x518, 0},
199 	{0x51a, 0},
200 	{0x51d, 0xff},
201 	{0x56f, 0x28},
202 	{0x575, 0x40},
203 	{0x5bc, 0x48},
204 	{0x5c1, 6},
205 	{0x5e5, 0x11},
206 	{0x5e6, 0x43},
207 	{0x5e7, 0x33},
208 	{0x5e8, 0x21},
209 	{0x5e9, 0x30},
210 	{0x5ea, 0x0},
211 	{0x5eb, 0xa5},
212 	{0x5ec, 0xff},
213 	{0x5fe, 2},
214 };
215 
216 static const struct rj54n1_reg_val bank_7[] = {
217 	{0x70a, 0},
218 	{0x714, 0xff},
219 	{0x715, 0xff},
220 	{0x716, 0x1f},
221 	{0x7FE, 2},
222 };
223 
224 static const struct rj54n1_reg_val bank_8[] = {
225 	{0x800, 0x00},
226 	{0x801, 0x01},
227 	{0x802, 0x61},
228 	{0x805, 0x00},
229 	{0x806, 0x00},
230 	{0x807, 0x00},
231 	{0x808, 0x00},
232 	{0x809, 0x01},
233 	{0x80A, 0x61},
234 	{0x80B, 0x00},
235 	{0x80C, 0x01},
236 	{0x80D, 0x00},
237 	{0x80E, 0x00},
238 	{0x80F, 0x00},
239 	{0x810, 0x00},
240 	{0x811, 0x01},
241 	{0x812, 0x61},
242 	{0x813, 0x00},
243 	{0x814, 0x11},
244 	{0x815, 0x00},
245 	{0x816, 0x41},
246 	{0x817, 0x00},
247 	{0x818, 0x51},
248 	{0x819, 0x01},
249 	{0x81A, 0x1F},
250 	{0x81B, 0x00},
251 	{0x81C, 0x01},
252 	{0x81D, 0x00},
253 	{0x81E, 0x11},
254 	{0x81F, 0x00},
255 	{0x820, 0x41},
256 	{0x821, 0x00},
257 	{0x822, 0x51},
258 	{0x823, 0x00},
259 	{0x824, 0x00},
260 	{0x825, 0x00},
261 	{0x826, 0x47},
262 	{0x827, 0x01},
263 	{0x828, 0x4F},
264 	{0x829, 0x00},
265 	{0x82A, 0x00},
266 	{0x82B, 0x00},
267 	{0x82C, 0x30},
268 	{0x82D, 0x00},
269 	{0x82E, 0x40},
270 	{0x82F, 0x00},
271 	{0x830, 0xB3},
272 	{0x831, 0x00},
273 	{0x832, 0xE3},
274 	{0x833, 0x00},
275 	{0x834, 0x00},
276 	{0x835, 0x00},
277 	{0x836, 0x00},
278 	{0x837, 0x00},
279 	{0x838, 0x00},
280 	{0x839, 0x01},
281 	{0x83A, 0x61},
282 	{0x83B, 0x00},
283 	{0x83C, 0x01},
284 	{0x83D, 0x00},
285 	{0x83E, 0x00},
286 	{0x83F, 0x00},
287 	{0x840, 0x00},
288 	{0x841, 0x01},
289 	{0x842, 0x61},
290 	{0x843, 0x00},
291 	{0x844, 0x1D},
292 	{0x845, 0x00},
293 	{0x846, 0x00},
294 	{0x847, 0x00},
295 	{0x848, 0x00},
296 	{0x849, 0x01},
297 	{0x84A, 0x1F},
298 	{0x84B, 0x00},
299 	{0x84C, 0x05},
300 	{0x84D, 0x00},
301 	{0x84E, 0x19},
302 	{0x84F, 0x01},
303 	{0x850, 0x21},
304 	{0x851, 0x01},
305 	{0x852, 0x5D},
306 	{0x853, 0x00},
307 	{0x854, 0x00},
308 	{0x855, 0x00},
309 	{0x856, 0x19},
310 	{0x857, 0x01},
311 	{0x858, 0x21},
312 	{0x859, 0x00},
313 	{0x85A, 0x00},
314 	{0x85B, 0x00},
315 	{0x85C, 0x00},
316 	{0x85D, 0x00},
317 	{0x85E, 0x00},
318 	{0x85F, 0x00},
319 	{0x860, 0xB3},
320 	{0x861, 0x00},
321 	{0x862, 0xE3},
322 	{0x863, 0x00},
323 	{0x864, 0x00},
324 	{0x865, 0x00},
325 	{0x866, 0x00},
326 	{0x867, 0x00},
327 	{0x868, 0x00},
328 	{0x869, 0xE2},
329 	{0x86A, 0x00},
330 	{0x86B, 0x01},
331 	{0x86C, 0x06},
332 	{0x86D, 0x00},
333 	{0x86E, 0x00},
334 	{0x86F, 0x00},
335 	{0x870, 0x60},
336 	{0x871, 0x8C},
337 	{0x872, 0x10},
338 	{0x873, 0x00},
339 	{0x874, 0xE0},
340 	{0x875, 0x00},
341 	{0x876, 0x27},
342 	{0x877, 0x01},
343 	{0x878, 0x00},
344 	{0x879, 0x00},
345 	{0x87A, 0x00},
346 	{0x87B, 0x03},
347 	{0x87C, 0x00},
348 	{0x87D, 0x00},
349 	{0x87E, 0x00},
350 	{0x87F, 0x00},
351 	{0x880, 0x00},
352 	{0x881, 0x00},
353 	{0x882, 0x00},
354 	{0x883, 0x00},
355 	{0x884, 0x00},
356 	{0x885, 0x00},
357 	{0x886, 0xF8},
358 	{0x887, 0x00},
359 	{0x888, 0x03},
360 	{0x889, 0x00},
361 	{0x88A, 0x64},
362 	{0x88B, 0x00},
363 	{0x88C, 0x03},
364 	{0x88D, 0x00},
365 	{0x88E, 0xB1},
366 	{0x88F, 0x00},
367 	{0x890, 0x03},
368 	{0x891, 0x01},
369 	{0x892, 0x1D},
370 	{0x893, 0x00},
371 	{0x894, 0x03},
372 	{0x895, 0x01},
373 	{0x896, 0x4B},
374 	{0x897, 0x00},
375 	{0x898, 0xE5},
376 	{0x899, 0x00},
377 	{0x89A, 0x01},
378 	{0x89B, 0x00},
379 	{0x89C, 0x01},
380 	{0x89D, 0x04},
381 	{0x89E, 0xC8},
382 	{0x89F, 0x00},
383 	{0x8A0, 0x01},
384 	{0x8A1, 0x01},
385 	{0x8A2, 0x61},
386 	{0x8A3, 0x00},
387 	{0x8A4, 0x01},
388 	{0x8A5, 0x00},
389 	{0x8A6, 0x00},
390 	{0x8A7, 0x00},
391 	{0x8A8, 0x00},
392 	{0x8A9, 0x00},
393 	{0x8AA, 0x7F},
394 	{0x8AB, 0x03},
395 	{0x8AC, 0x00},
396 	{0x8AD, 0x00},
397 	{0x8AE, 0x00},
398 	{0x8AF, 0x00},
399 	{0x8B0, 0x00},
400 	{0x8B1, 0x00},
401 	{0x8B6, 0x00},
402 	{0x8B7, 0x01},
403 	{0x8B8, 0x00},
404 	{0x8B9, 0x00},
405 	{0x8BA, 0x02},
406 	{0x8BB, 0x00},
407 	{0x8BC, 0xFF},
408 	{0x8BD, 0x00},
409 	{0x8FE, 2},
410 };
411 
412 static const struct rj54n1_reg_val bank_10[] = {
413 	{0x10bf, 0x69}
414 };
415 
416 /* Clock dividers - these are default register values, divider = register + 1 */
417 static const struct rj54n1_clock_div clk_div = {
418 	.ratio_tg	= 3 /* default: 5 */,
419 	.ratio_t	= 4 /* default: 1 */,
420 	.ratio_r	= 4 /* default: 0 */,
421 	.ratio_op	= 1 /* default: 5 */,
422 	.ratio_o	= 9 /* default: 0 */,
423 };
424 
425 static struct rj54n1 *to_rj54n1(const struct i2c_client *client)
426 {
427 	return container_of(i2c_get_clientdata(client), struct rj54n1, subdev);
428 }
429 
430 static int reg_read(struct i2c_client *client, const u16 reg)
431 {
432 	struct rj54n1 *rj54n1 = to_rj54n1(client);
433 	int ret;
434 
435 	/* set bank */
436 	if (rj54n1->bank != reg >> 8) {
437 		dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
438 		ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
439 		if (ret < 0)
440 			return ret;
441 		rj54n1->bank = reg >> 8;
442 	}
443 	return i2c_smbus_read_byte_data(client, reg & 0xff);
444 }
445 
446 static int reg_write(struct i2c_client *client, const u16 reg,
447 		     const u8 data)
448 {
449 	struct rj54n1 *rj54n1 = to_rj54n1(client);
450 	int ret;
451 
452 	/* set bank */
453 	if (rj54n1->bank != reg >> 8) {
454 		dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
455 		ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
456 		if (ret < 0)
457 			return ret;
458 		rj54n1->bank = reg >> 8;
459 	}
460 	dev_dbg(&client->dev, "[0x%x] = 0x%x\n", reg & 0xff, data);
461 	return i2c_smbus_write_byte_data(client, reg & 0xff, data);
462 }
463 
464 static int reg_set(struct i2c_client *client, const u16 reg,
465 		   const u8 data, const u8 mask)
466 {
467 	int ret;
468 
469 	ret = reg_read(client, reg);
470 	if (ret < 0)
471 		return ret;
472 	return reg_write(client, reg, (ret & ~mask) | (data & mask));
473 }
474 
475 static int reg_write_multiple(struct i2c_client *client,
476 			      const struct rj54n1_reg_val *rv, const int n)
477 {
478 	int i, ret;
479 
480 	for (i = 0; i < n; i++) {
481 		ret = reg_write(client, rv->reg, rv->val);
482 		if (ret < 0)
483 			return ret;
484 		rv++;
485 	}
486 
487 	return 0;
488 }
489 
490 static int rj54n1_enum_mbus_code(struct v4l2_subdev *sd,
491 		struct v4l2_subdev_pad_config *cfg,
492 		struct v4l2_subdev_mbus_code_enum *code)
493 {
494 	if (code->pad || code->index >= ARRAY_SIZE(rj54n1_colour_fmts))
495 		return -EINVAL;
496 
497 	code->code = rj54n1_colour_fmts[code->index].code;
498 	return 0;
499 }
500 
501 static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable)
502 {
503 	struct i2c_client *client = v4l2_get_subdevdata(sd);
504 
505 	/* Switch between preview and still shot modes */
506 	return reg_set(client, RJ54N1_STILL_CONTROL, (!enable) << 7, 0x80);
507 }
508 
509 static int rj54n1_set_rect(struct i2c_client *client,
510 			   u16 reg_x, u16 reg_y, u16 reg_xy,
511 			   u32 width, u32 height)
512 {
513 	int ret;
514 
515 	ret = reg_write(client, reg_xy,
516 			((width >> 4) & 0x70) |
517 			((height >> 8) & 7));
518 
519 	if (!ret)
520 		ret = reg_write(client, reg_x, width & 0xff);
521 	if (!ret)
522 		ret = reg_write(client, reg_y, height & 0xff);
523 
524 	return ret;
525 }
526 
527 /*
528  * Some commands, specifically certain initialisation sequences, require
529  * a commit operation.
530  */
531 static int rj54n1_commit(struct i2c_client *client)
532 {
533 	int ret = reg_write(client, RJ54N1_INIT_START, 1);
534 	msleep(10);
535 	if (!ret)
536 		ret = reg_write(client, RJ54N1_INIT_START, 0);
537 	return ret;
538 }
539 
540 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
541 			       s32 *out_w, s32 *out_h);
542 
543 static int rj54n1_set_selection(struct v4l2_subdev *sd,
544 				struct v4l2_subdev_pad_config *cfg,
545 				struct v4l2_subdev_selection *sel)
546 {
547 	struct i2c_client *client = v4l2_get_subdevdata(sd);
548 	struct rj54n1 *rj54n1 = to_rj54n1(client);
549 	const struct v4l2_rect *rect = &sel->r;
550 	int output_w, output_h, input_w = rect->width, input_h = rect->height;
551 	int ret;
552 
553 	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
554 	    sel->target != V4L2_SEL_TGT_CROP)
555 		return -EINVAL;
556 
557 	/* arbitrary minimum width and height, edges unimportant */
558 	v4l_bound_align_image(&input_w, 8, RJ54N1_MAX_WIDTH, 0,
559 			      &input_h, 8, RJ54N1_MAX_HEIGHT, 0, 0);
560 
561 	output_w = (input_w * 1024 + rj54n1->resize / 2) / rj54n1->resize;
562 	output_h = (input_h * 1024 + rj54n1->resize / 2) / rj54n1->resize;
563 
564 	dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n",
565 		input_w, input_h, rj54n1->resize, output_w, output_h);
566 
567 	ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
568 	if (ret < 0)
569 		return ret;
570 
571 	rj54n1->width		= output_w;
572 	rj54n1->height		= output_h;
573 	rj54n1->resize		= ret;
574 	rj54n1->rect.width	= input_w;
575 	rj54n1->rect.height	= input_h;
576 
577 	return 0;
578 }
579 
580 static int rj54n1_get_selection(struct v4l2_subdev *sd,
581 				struct v4l2_subdev_pad_config *cfg,
582 				struct v4l2_subdev_selection *sel)
583 {
584 	struct i2c_client *client = v4l2_get_subdevdata(sd);
585 	struct rj54n1 *rj54n1 = to_rj54n1(client);
586 
587 	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
588 		return -EINVAL;
589 
590 	switch (sel->target) {
591 	case V4L2_SEL_TGT_CROP_BOUNDS:
592 	case V4L2_SEL_TGT_CROP_DEFAULT:
593 		sel->r.left = RJ54N1_COLUMN_SKIP;
594 		sel->r.top = RJ54N1_ROW_SKIP;
595 		sel->r.width = RJ54N1_MAX_WIDTH;
596 		sel->r.height = RJ54N1_MAX_HEIGHT;
597 		return 0;
598 	case V4L2_SEL_TGT_CROP:
599 		sel->r = rj54n1->rect;
600 		return 0;
601 	default:
602 		return -EINVAL;
603 	}
604 }
605 
606 static int rj54n1_get_fmt(struct v4l2_subdev *sd,
607 		struct v4l2_subdev_pad_config *cfg,
608 		struct v4l2_subdev_format *format)
609 {
610 	struct v4l2_mbus_framefmt *mf = &format->format;
611 	struct i2c_client *client = v4l2_get_subdevdata(sd);
612 	struct rj54n1 *rj54n1 = to_rj54n1(client);
613 
614 	if (format->pad)
615 		return -EINVAL;
616 
617 	mf->code	= rj54n1->fmt->code;
618 	mf->colorspace	= rj54n1->fmt->colorspace;
619 	mf->ycbcr_enc	= V4L2_YCBCR_ENC_601;
620 	mf->xfer_func	= V4L2_XFER_FUNC_SRGB;
621 	mf->quantization = V4L2_QUANTIZATION_DEFAULT;
622 	mf->field	= V4L2_FIELD_NONE;
623 	mf->width	= rj54n1->width;
624 	mf->height	= rj54n1->height;
625 
626 	return 0;
627 }
628 
629 /*
630  * The actual geometry configuration routine. It scales the input window into
631  * the output one, updates the window sizes and returns an error or the resize
632  * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
633  */
634 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
635 			       s32 *out_w, s32 *out_h)
636 {
637 	struct i2c_client *client = v4l2_get_subdevdata(sd);
638 	struct rj54n1 *rj54n1 = to_rj54n1(client);
639 	unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
640 		output_w = *out_w, output_h = *out_h;
641 	u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
642 	unsigned int peak, peak_50, peak_60;
643 	int ret;
644 
645 	/*
646 	 * We have a problem with crops, where the window is larger than 512x384
647 	 * and output window is larger than a half of the input one. In this
648 	 * case we have to either reduce the input window to equal or below
649 	 * 512x384 or the output window to equal or below 1/2 of the input.
650 	 */
651 	if (output_w > max(512U, input_w / 2)) {
652 		if (2 * output_w > RJ54N1_MAX_WIDTH) {
653 			input_w = RJ54N1_MAX_WIDTH;
654 			output_w = RJ54N1_MAX_WIDTH / 2;
655 		} else {
656 			input_w = output_w * 2;
657 		}
658 
659 		dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n",
660 			input_w, output_w);
661 	}
662 
663 	if (output_h > max(384U, input_h / 2)) {
664 		if (2 * output_h > RJ54N1_MAX_HEIGHT) {
665 			input_h = RJ54N1_MAX_HEIGHT;
666 			output_h = RJ54N1_MAX_HEIGHT / 2;
667 		} else {
668 			input_h = output_h * 2;
669 		}
670 
671 		dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n",
672 			input_h, output_h);
673 	}
674 
675 	/* Idea: use the read mode for snapshots, handle separate geometries */
676 	ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
677 			      RJ54N1_Y_OUTPUT_SIZE_S_L,
678 			      RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
679 	if (!ret)
680 		ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
681 			      RJ54N1_Y_OUTPUT_SIZE_P_L,
682 			      RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
683 
684 	if (ret < 0)
685 		return ret;
686 
687 	if (output_w > input_w && output_h > input_h) {
688 		input_w = output_w;
689 		input_h = output_h;
690 
691 		resize = 1024;
692 	} else {
693 		unsigned int resize_x, resize_y;
694 		resize_x = (input_w * 1024 + output_w / 2) / output_w;
695 		resize_y = (input_h * 1024 + output_h / 2) / output_h;
696 
697 		/* We want max(resize_x, resize_y), check if it still fits */
698 		if (resize_x > resize_y &&
699 		    (output_h * resize_x + 512) / 1024 > RJ54N1_MAX_HEIGHT)
700 			resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2) /
701 				output_h;
702 		else if (resize_y > resize_x &&
703 			 (output_w * resize_y + 512) / 1024 > RJ54N1_MAX_WIDTH)
704 			resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2) /
705 				output_w;
706 		else
707 			resize = max(resize_x, resize_y);
708 
709 		/* Prohibited value ranges */
710 		switch (resize) {
711 		case 2040 ... 2047:
712 			resize = 2039;
713 			break;
714 		case 4080 ... 4095:
715 			resize = 4079;
716 			break;
717 		case 8160 ... 8191:
718 			resize = 8159;
719 			break;
720 		case 16320 ... 16384:
721 			resize = 16319;
722 		}
723 	}
724 
725 	/* Set scaling */
726 	ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
727 	if (!ret)
728 		ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
729 
730 	if (ret < 0)
731 		return ret;
732 
733 	/*
734 	 * Configure a skipping bitmask. The sensor will select a skipping value
735 	 * among set bits automatically. This is very unclear in the datasheet
736 	 * too. I was told, in this register one enables all skipping values,
737 	 * that are required for a specific resize, and the camera selects
738 	 * automatically, which ones to use. But it is unclear how to identify,
739 	 * which cropping values are needed. Secondly, why don't we just set all
740 	 * bits and let the camera choose? Would it increase processing time and
741 	 * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
742 	 * improve the image quality or stability for larger frames (see comment
743 	 * above), but I didn't check the framerate.
744 	 */
745 	skip = min(resize / 1024, 15U);
746 
747 	inc_sel = 1 << skip;
748 
749 	if (inc_sel <= 2)
750 		inc_sel = 0xc;
751 	else if (resize & 1023 && skip < 15)
752 		inc_sel |= 1 << (skip + 1);
753 
754 	ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
755 	if (!ret)
756 		ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
757 
758 	if (!rj54n1->auto_wb) {
759 		/* Auto white balance window */
760 		wb_left	  = output_w / 16;
761 		wb_right  = (3 * output_w / 4 - 3) / 4;
762 		wb_top	  = output_h / 16;
763 		wb_bottom = (3 * output_h / 4 - 3) / 4;
764 		wb_bit8	  = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
765 			((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
766 
767 		if (!ret)
768 			ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
769 		if (!ret)
770 			ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
771 		if (!ret)
772 			ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
773 		if (!ret)
774 			ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
775 		if (!ret)
776 			ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
777 	}
778 
779 	/* Antiflicker */
780 	peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14) * resize / rj54n1->tgclk_mhz /
781 		10000;
782 	peak_50 = peak / 6;
783 	peak_60 = peak / 5;
784 
785 	if (!ret)
786 		ret = reg_write(client, RJ54N1_PEAK_H,
787 				((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
788 	if (!ret)
789 		ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
790 	if (!ret)
791 		ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
792 	if (!ret)
793 		ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150);
794 
795 	/* Start resizing */
796 	if (!ret)
797 		ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
798 				RESIZE_HOLD_SEL | RESIZE_GO | 1);
799 
800 	if (ret < 0)
801 		return ret;
802 
803 	/* Constant taken from manufacturer's example */
804 	msleep(230);
805 
806 	ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
807 	if (ret < 0)
808 		return ret;
809 
810 	*in_w = (output_w * resize + 512) / 1024;
811 	*in_h = (output_h * resize + 512) / 1024;
812 	*out_w = output_w;
813 	*out_h = output_h;
814 
815 	dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
816 		*in_w, *in_h, resize, output_w, output_h, skip);
817 
818 	return resize;
819 }
820 
821 static int rj54n1_set_clock(struct i2c_client *client)
822 {
823 	struct rj54n1 *rj54n1 = to_rj54n1(client);
824 	int ret;
825 
826 	/* Enable external clock */
827 	ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
828 	/* Leave stand-by. Note: use this when implementing suspend / resume */
829 	if (!ret)
830 		ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
831 
832 	if (!ret)
833 		ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
834 	if (!ret)
835 		ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
836 
837 	/* TGCLK dividers */
838 	if (!ret)
839 		ret = reg_write(client, RJ54N1_RATIO_TG,
840 				rj54n1->clk_div.ratio_tg);
841 	if (!ret)
842 		ret = reg_write(client, RJ54N1_RATIO_T,
843 				rj54n1->clk_div.ratio_t);
844 	if (!ret)
845 		ret = reg_write(client, RJ54N1_RATIO_R,
846 				rj54n1->clk_div.ratio_r);
847 
848 	/* Enable TGCLK & RAMP */
849 	if (!ret)
850 		ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
851 
852 	/* Disable clock output */
853 	if (!ret)
854 		ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
855 
856 	/* Set divisors */
857 	if (!ret)
858 		ret = reg_write(client, RJ54N1_RATIO_OP,
859 				rj54n1->clk_div.ratio_op);
860 	if (!ret)
861 		ret = reg_write(client, RJ54N1_RATIO_O,
862 				rj54n1->clk_div.ratio_o);
863 
864 	/* Enable OCLK */
865 	if (!ret)
866 		ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
867 
868 	/* Use PLL for Timing Generator, write 2 to reserved bits */
869 	if (!ret)
870 		ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
871 
872 	/* Take sensor out of reset */
873 	if (!ret)
874 		ret = reg_write(client, RJ54N1_RESET_STANDBY,
875 				E_EXCLK | SEN_RSTX);
876 	/* Enable PLL */
877 	if (!ret)
878 		ret = reg_write(client, RJ54N1_PLL_EN, 1);
879 
880 	/* Wait for PLL to stabilise */
881 	msleep(10);
882 
883 	/* Enable clock to frequency divider */
884 	if (!ret)
885 		ret = reg_write(client, RJ54N1_CLK_RST, 1);
886 
887 	if (!ret)
888 		ret = reg_read(client, RJ54N1_CLK_RST);
889 	if (ret != 1) {
890 		dev_err(&client->dev,
891 			"Resetting RJ54N1CB0C clock failed: %d!\n", ret);
892 		return -EIO;
893 	}
894 
895 	/* Start the PLL */
896 	ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
897 
898 	/* Enable OCLK */
899 	if (!ret)
900 		ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
901 
902 	return ret;
903 }
904 
905 static int rj54n1_reg_init(struct i2c_client *client)
906 {
907 	struct rj54n1 *rj54n1 = to_rj54n1(client);
908 	int ret = rj54n1_set_clock(client);
909 
910 	if (!ret)
911 		ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
912 	if (!ret)
913 		ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
914 
915 	/* Set binning divisors */
916 	if (!ret)
917 		ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
918 	if (!ret)
919 		ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
920 
921 	/* Switch to fixed resize mode */
922 	if (!ret)
923 		ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
924 				RESIZE_HOLD_SEL | 1);
925 
926 	/* Set gain */
927 	if (!ret)
928 		ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
929 
930 	/*
931 	 * Mirror the image back: default is upside down and left-to-right...
932 	 * Set manual preview / still shot switching
933 	 */
934 	if (!ret)
935 		ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0x27);
936 
937 	if (!ret)
938 		ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
939 
940 	/* Auto exposure area */
941 	if (!ret)
942 		ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0x80);
943 	/* Check current auto WB config */
944 	if (!ret)
945 		ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
946 	if (ret >= 0) {
947 		rj54n1->auto_wb = ret & 0x80;
948 		ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
949 	}
950 	if (!ret)
951 		ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
952 
953 	if (!ret)
954 		ret = reg_write(client, RJ54N1_RESET_STANDBY,
955 				E_EXCLK | DSP_RSTX | SEN_RSTX);
956 
957 	/* Commit init */
958 	if (!ret)
959 		ret = rj54n1_commit(client);
960 
961 	/* Take DSP, TG, sensor out of reset */
962 	if (!ret)
963 		ret = reg_write(client, RJ54N1_RESET_STANDBY,
964 				E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
965 
966 	/* Start register update? Same register as 0x?FE in many bank_* sets */
967 	if (!ret)
968 		ret = reg_write(client, RJ54N1_FWFLG, 2);
969 
970 	/* Constant taken from manufacturer's example */
971 	msleep(700);
972 
973 	return ret;
974 }
975 
976 static int rj54n1_set_fmt(struct v4l2_subdev *sd,
977 		struct v4l2_subdev_pad_config *cfg,
978 		struct v4l2_subdev_format *format)
979 {
980 	struct v4l2_mbus_framefmt *mf = &format->format;
981 	struct i2c_client *client = v4l2_get_subdevdata(sd);
982 	struct rj54n1 *rj54n1 = to_rj54n1(client);
983 	const struct rj54n1_datafmt *fmt;
984 	int output_w, output_h, max_w, max_h,
985 		input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
986 	int align = mf->code == MEDIA_BUS_FMT_SBGGR10_1X10 ||
987 		mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE ||
988 		mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE ||
989 		mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE ||
990 		mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE;
991 	int ret;
992 
993 	if (format->pad)
994 		return -EINVAL;
995 
996 	dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
997 		__func__, mf->code, mf->width, mf->height);
998 
999 	fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1000 				  ARRAY_SIZE(rj54n1_colour_fmts));
1001 	if (!fmt) {
1002 		fmt = rj54n1->fmt;
1003 		mf->code = fmt->code;
1004 	}
1005 
1006 	mf->field	= V4L2_FIELD_NONE;
1007 	mf->colorspace	= fmt->colorspace;
1008 
1009 	v4l_bound_align_image(&mf->width, 112, RJ54N1_MAX_WIDTH, align,
1010 			      &mf->height, 84, RJ54N1_MAX_HEIGHT, align, 0);
1011 
1012 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1013 		cfg->try_fmt = *mf;
1014 		return 0;
1015 	}
1016 
1017 	/*
1018 	 * Verify if the sensor has just been powered on. TODO: replace this
1019 	 * with proper PM, when a suitable API is available.
1020 	 */
1021 	ret = reg_read(client, RJ54N1_RESET_STANDBY);
1022 	if (ret < 0)
1023 		return ret;
1024 
1025 	if (!(ret & E_EXCLK)) {
1026 		ret = rj54n1_reg_init(client);
1027 		if (ret < 0)
1028 			return ret;
1029 	}
1030 
1031 	/* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
1032 	switch (mf->code) {
1033 	case MEDIA_BUS_FMT_YUYV8_2X8:
1034 		ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1035 		if (!ret)
1036 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1037 		break;
1038 	case MEDIA_BUS_FMT_YVYU8_2X8:
1039 		ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1040 		if (!ret)
1041 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1042 		break;
1043 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
1044 		ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1045 		if (!ret)
1046 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1047 		break;
1048 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
1049 		ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1050 		if (!ret)
1051 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1052 		break;
1053 	case MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE:
1054 		ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1055 		if (!ret)
1056 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1057 		if (!ret)
1058 			ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1059 		break;
1060 	case MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE:
1061 		ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1062 		if (!ret)
1063 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1064 		if (!ret)
1065 			ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1066 		break;
1067 	case MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE:
1068 		ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1069 		if (!ret)
1070 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1071 		if (!ret)
1072 			ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1073 		break;
1074 	case MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE:
1075 		ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1076 		if (!ret)
1077 			ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1078 		if (!ret)
1079 			ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1080 		break;
1081 	case MEDIA_BUS_FMT_SBGGR10_1X10:
1082 		ret = reg_write(client, RJ54N1_OUT_SEL, 5);
1083 		break;
1084 	default:
1085 		ret = -EINVAL;
1086 	}
1087 
1088 	/* Special case: a raw mode with 10 bits of data per clock tick */
1089 	if (!ret)
1090 		ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
1091 			      (mf->code == MEDIA_BUS_FMT_SBGGR10_1X10) << 1, 2);
1092 
1093 	if (ret < 0)
1094 		return ret;
1095 
1096 	/* Supported scales 1:1 >= scale > 1:16 */
1097 	max_w = mf->width * (16 * 1024 - 1) / 1024;
1098 	if (input_w > max_w)
1099 		input_w = max_w;
1100 	max_h = mf->height * (16 * 1024 - 1) / 1024;
1101 	if (input_h > max_h)
1102 		input_h = max_h;
1103 
1104 	output_w = mf->width;
1105 	output_h = mf->height;
1106 
1107 	ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
1108 	if (ret < 0)
1109 		return ret;
1110 
1111 	fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1112 				  ARRAY_SIZE(rj54n1_colour_fmts));
1113 
1114 	rj54n1->fmt		= fmt;
1115 	rj54n1->resize		= ret;
1116 	rj54n1->rect.width	= input_w;
1117 	rj54n1->rect.height	= input_h;
1118 	rj54n1->width		= output_w;
1119 	rj54n1->height		= output_h;
1120 
1121 	mf->width		= output_w;
1122 	mf->height		= output_h;
1123 	mf->field		= V4L2_FIELD_NONE;
1124 	mf->colorspace		= fmt->colorspace;
1125 
1126 	return 0;
1127 }
1128 
1129 #ifdef CONFIG_VIDEO_ADV_DEBUG
1130 static int rj54n1_g_register(struct v4l2_subdev *sd,
1131 			     struct v4l2_dbg_register *reg)
1132 {
1133 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1134 
1135 	if (reg->reg < 0x400 || reg->reg > 0x1fff)
1136 		/* Registers > 0x0800 are only available from Sharp support */
1137 		return -EINVAL;
1138 
1139 	reg->size = 1;
1140 	reg->val = reg_read(client, reg->reg);
1141 
1142 	if (reg->val > 0xff)
1143 		return -EIO;
1144 
1145 	return 0;
1146 }
1147 
1148 static int rj54n1_s_register(struct v4l2_subdev *sd,
1149 			     const struct v4l2_dbg_register *reg)
1150 {
1151 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1152 
1153 	if (reg->reg < 0x400 || reg->reg > 0x1fff)
1154 		/* Registers >= 0x0800 are only available from Sharp support */
1155 		return -EINVAL;
1156 
1157 	if (reg_write(client, reg->reg, reg->val) < 0)
1158 		return -EIO;
1159 
1160 	return 0;
1161 }
1162 #endif
1163 
1164 static int rj54n1_s_power(struct v4l2_subdev *sd, int on)
1165 {
1166 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1167 	struct rj54n1 *rj54n1 = to_rj54n1(client);
1168 
1169 	if (on) {
1170 		if (rj54n1->pwup_gpio)
1171 			gpiod_set_value(rj54n1->pwup_gpio, 1);
1172 		if (rj54n1->enable_gpio)
1173 			gpiod_set_value(rj54n1->enable_gpio, 1);
1174 
1175 		msleep(1);
1176 
1177 		return clk_prepare_enable(rj54n1->clk);
1178 	}
1179 
1180 	clk_disable_unprepare(rj54n1->clk);
1181 
1182 	if (rj54n1->enable_gpio)
1183 		gpiod_set_value(rj54n1->enable_gpio, 0);
1184 	if (rj54n1->pwup_gpio)
1185 		gpiod_set_value(rj54n1->pwup_gpio, 0);
1186 
1187 	return 0;
1188 }
1189 
1190 static int rj54n1_s_ctrl(struct v4l2_ctrl *ctrl)
1191 {
1192 	struct rj54n1 *rj54n1 = container_of(ctrl->handler, struct rj54n1, hdl);
1193 	struct v4l2_subdev *sd = &rj54n1->subdev;
1194 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1195 	int data;
1196 
1197 	switch (ctrl->id) {
1198 	case V4L2_CID_VFLIP:
1199 		if (ctrl->val)
1200 			data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
1201 		else
1202 			data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
1203 		if (data < 0)
1204 			return -EIO;
1205 		return 0;
1206 	case V4L2_CID_HFLIP:
1207 		if (ctrl->val)
1208 			data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
1209 		else
1210 			data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
1211 		if (data < 0)
1212 			return -EIO;
1213 		return 0;
1214 	case V4L2_CID_GAIN:
1215 		if (reg_write(client, RJ54N1_Y_GAIN, ctrl->val * 2) < 0)
1216 			return -EIO;
1217 		return 0;
1218 	case V4L2_CID_AUTO_WHITE_BALANCE:
1219 		/* Auto WB area - whole image */
1220 		if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->val << 7,
1221 			    0x80) < 0)
1222 			return -EIO;
1223 		rj54n1->auto_wb = ctrl->val;
1224 		return 0;
1225 	}
1226 
1227 	return -EINVAL;
1228 }
1229 
1230 static const struct v4l2_ctrl_ops rj54n1_ctrl_ops = {
1231 	.s_ctrl = rj54n1_s_ctrl,
1232 };
1233 
1234 static const struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
1235 #ifdef CONFIG_VIDEO_ADV_DEBUG
1236 	.g_register	= rj54n1_g_register,
1237 	.s_register	= rj54n1_s_register,
1238 #endif
1239 	.s_power	= rj54n1_s_power,
1240 };
1241 
1242 static const struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
1243 	.s_stream	= rj54n1_s_stream,
1244 };
1245 
1246 static const struct v4l2_subdev_pad_ops rj54n1_subdev_pad_ops = {
1247 	.enum_mbus_code = rj54n1_enum_mbus_code,
1248 	.get_selection	= rj54n1_get_selection,
1249 	.set_selection	= rj54n1_set_selection,
1250 	.get_fmt	= rj54n1_get_fmt,
1251 	.set_fmt	= rj54n1_set_fmt,
1252 };
1253 
1254 static const struct v4l2_subdev_ops rj54n1_subdev_ops = {
1255 	.core	= &rj54n1_subdev_core_ops,
1256 	.video	= &rj54n1_subdev_video_ops,
1257 	.pad	= &rj54n1_subdev_pad_ops,
1258 };
1259 
1260 /*
1261  * Interface active, can use i2c. If it fails, it can indeed mean, that
1262  * this wasn't our capture interface, so, we wait for the right one
1263  */
1264 static int rj54n1_video_probe(struct i2c_client *client,
1265 			      struct rj54n1_pdata *priv)
1266 {
1267 	struct rj54n1 *rj54n1 = to_rj54n1(client);
1268 	int data1, data2;
1269 	int ret;
1270 
1271 	ret = rj54n1_s_power(&rj54n1->subdev, 1);
1272 	if (ret < 0)
1273 		return ret;
1274 
1275 	/* Read out the chip version register */
1276 	data1 = reg_read(client, RJ54N1_DEV_CODE);
1277 	data2 = reg_read(client, RJ54N1_DEV_CODE2);
1278 
1279 	if (data1 != 0x51 || data2 != 0x10) {
1280 		ret = -ENODEV;
1281 		dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
1282 			 data1, data2);
1283 		goto done;
1284 	}
1285 
1286 	/* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
1287 	ret = reg_write(client, RJ54N1_IOC, priv->ioctl_high << 7);
1288 	if (ret < 0)
1289 		goto done;
1290 
1291 	dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
1292 		 data1, data2);
1293 
1294 	ret = v4l2_ctrl_handler_setup(&rj54n1->hdl);
1295 
1296 done:
1297 	rj54n1_s_power(&rj54n1->subdev, 0);
1298 	return ret;
1299 }
1300 
1301 static int rj54n1_probe(struct i2c_client *client,
1302 			const struct i2c_device_id *did)
1303 {
1304 	struct rj54n1 *rj54n1;
1305 	struct i2c_adapter *adapter = client->adapter;
1306 	struct rj54n1_pdata *rj54n1_priv;
1307 	int ret;
1308 
1309 	if (!client->dev.platform_data) {
1310 		dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n");
1311 		return -EINVAL;
1312 	}
1313 
1314 	rj54n1_priv = client->dev.platform_data;
1315 
1316 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1317 		dev_warn(&adapter->dev,
1318 			 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
1319 		return -EIO;
1320 	}
1321 
1322 	rj54n1 = devm_kzalloc(&client->dev, sizeof(struct rj54n1), GFP_KERNEL);
1323 	if (!rj54n1)
1324 		return -ENOMEM;
1325 
1326 	v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
1327 	v4l2_ctrl_handler_init(&rj54n1->hdl, 4);
1328 	v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
1329 			V4L2_CID_VFLIP, 0, 1, 1, 0);
1330 	v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
1331 			V4L2_CID_HFLIP, 0, 1, 1, 0);
1332 	v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
1333 			V4L2_CID_GAIN, 0, 127, 1, 66);
1334 	v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
1335 			V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1336 	rj54n1->subdev.ctrl_handler = &rj54n1->hdl;
1337 	if (rj54n1->hdl.error)
1338 		return rj54n1->hdl.error;
1339 
1340 	rj54n1->clk_div		= clk_div;
1341 	rj54n1->rect.left	= RJ54N1_COLUMN_SKIP;
1342 	rj54n1->rect.top	= RJ54N1_ROW_SKIP;
1343 	rj54n1->rect.width	= RJ54N1_MAX_WIDTH;
1344 	rj54n1->rect.height	= RJ54N1_MAX_HEIGHT;
1345 	rj54n1->width		= RJ54N1_MAX_WIDTH;
1346 	rj54n1->height		= RJ54N1_MAX_HEIGHT;
1347 	rj54n1->fmt		= &rj54n1_colour_fmts[0];
1348 	rj54n1->resize		= 1024;
1349 	rj54n1->tgclk_mhz	= (rj54n1_priv->mclk_freq / PLL_L * PLL_N) /
1350 		(clk_div.ratio_tg + 1) / (clk_div.ratio_t + 1);
1351 
1352 	rj54n1->clk = clk_get(&client->dev, NULL);
1353 	if (IS_ERR(rj54n1->clk)) {
1354 		ret = PTR_ERR(rj54n1->clk);
1355 		goto err_free_ctrl;
1356 	}
1357 
1358 	rj54n1->pwup_gpio = gpiod_get_optional(&client->dev, "powerup",
1359 					       GPIOD_OUT_LOW);
1360 	if (IS_ERR(rj54n1->pwup_gpio)) {
1361 		dev_info(&client->dev, "Unable to get GPIO \"powerup\": %ld\n",
1362 			 PTR_ERR(rj54n1->pwup_gpio));
1363 		ret = PTR_ERR(rj54n1->pwup_gpio);
1364 		goto err_clk_put;
1365 	}
1366 
1367 	rj54n1->enable_gpio = gpiod_get_optional(&client->dev, "enable",
1368 						 GPIOD_OUT_LOW);
1369 	if (IS_ERR(rj54n1->enable_gpio)) {
1370 		dev_info(&client->dev, "Unable to get GPIO \"enable\": %ld\n",
1371 			 PTR_ERR(rj54n1->enable_gpio));
1372 		ret = PTR_ERR(rj54n1->enable_gpio);
1373 		goto err_gpio_put;
1374 	}
1375 
1376 	ret = rj54n1_video_probe(client, rj54n1_priv);
1377 	if (ret < 0)
1378 		goto err_gpio_put;
1379 
1380 	ret = v4l2_async_register_subdev(&rj54n1->subdev);
1381 	if (ret)
1382 		goto err_gpio_put;
1383 
1384 	return 0;
1385 
1386 err_gpio_put:
1387 	if (rj54n1->enable_gpio)
1388 		gpiod_put(rj54n1->enable_gpio);
1389 
1390 	if (rj54n1->pwup_gpio)
1391 		gpiod_put(rj54n1->pwup_gpio);
1392 
1393 err_clk_put:
1394 	clk_put(rj54n1->clk);
1395 
1396 err_free_ctrl:
1397 	v4l2_ctrl_handler_free(&rj54n1->hdl);
1398 
1399 	return ret;
1400 }
1401 
1402 static int rj54n1_remove(struct i2c_client *client)
1403 {
1404 	struct rj54n1 *rj54n1 = to_rj54n1(client);
1405 
1406 	if (rj54n1->enable_gpio)
1407 		gpiod_put(rj54n1->enable_gpio);
1408 	if (rj54n1->pwup_gpio)
1409 		gpiod_put(rj54n1->pwup_gpio);
1410 
1411 	clk_put(rj54n1->clk);
1412 	v4l2_ctrl_handler_free(&rj54n1->hdl);
1413 	v4l2_async_unregister_subdev(&rj54n1->subdev);
1414 
1415 	return 0;
1416 }
1417 
1418 static const struct i2c_device_id rj54n1_id[] = {
1419 	{ "rj54n1cb0c", 0 },
1420 	{ }
1421 };
1422 MODULE_DEVICE_TABLE(i2c, rj54n1_id);
1423 
1424 static struct i2c_driver rj54n1_i2c_driver = {
1425 	.driver = {
1426 		.name = "rj54n1cb0c",
1427 	},
1428 	.probe		= rj54n1_probe,
1429 	.remove		= rj54n1_remove,
1430 	.id_table	= rj54n1_id,
1431 };
1432 
1433 module_i2c_driver(rj54n1_i2c_driver);
1434 
1435 MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
1436 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1437 MODULE_LICENSE("GPL v2");
1438