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