[media] Revert "[media] smiapp: Don't compile of_read_number() if CONFIG_OF isn't...
[deliverable/linux.git] / drivers / media / i2c / s5k5baf.c
CommitLineData
7d459937
AH
1/*
2 * Driver for Samsung S5K5BAF UXGA 1/5" 2M CMOS Image Sensor
3 * with embedded SoC ISP.
4 *
5 * Copyright (C) 2013, Samsung Electronics Co., Ltd.
6 * Andrzej Hajda <a.hajda@samsung.com>
7 *
8 * Based on S5K6AA driver authored by Sylwester Nawrocki
9 * Copyright (C) 2013, Samsung Electronics Co., Ltd.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/firmware.h>
19#include <linux/gpio.h>
20#include <linux/i2c.h>
21#include <linux/media.h>
22#include <linux/module.h>
23#include <linux/of_gpio.h>
fd9fdb78 24#include <linux/of_graph.h>
7d459937
AH
25#include <linux/regulator/consumer.h>
26#include <linux/slab.h>
27
28#include <media/media-entity.h>
29#include <media/v4l2-ctrls.h>
30#include <media/v4l2-device.h>
31#include <media/v4l2-subdev.h>
32#include <media/v4l2-mediabus.h>
33#include <media/v4l2-of.h>
34
35static int debug;
36module_param(debug, int, 0644);
37
38#define S5K5BAF_DRIVER_NAME "s5k5baf"
39#define S5K5BAF_DEFAULT_MCLK_FREQ 24000000U
40#define S5K5BAF_CLK_NAME "mclk"
41
42#define S5K5BAF_FW_FILENAME "s5k5baf-cfg.bin"
43#define S5K5BAF_FW_TAG "SF00"
44#define S5K5BAG_FW_TAG_LEN 2
45#define S5K5BAG_FW_MAX_COUNT 16
46
47#define S5K5BAF_CIS_WIDTH 1600
48#define S5K5BAF_CIS_HEIGHT 1200
49#define S5K5BAF_WIN_WIDTH_MIN 8
50#define S5K5BAF_WIN_HEIGHT_MIN 8
51#define S5K5BAF_GAIN_RED_DEF 127
52#define S5K5BAF_GAIN_GREEN_DEF 95
53#define S5K5BAF_GAIN_BLUE_DEF 180
54/* Default number of MIPI CSI-2 data lanes used */
55#define S5K5BAF_DEF_NUM_LANES 1
56
57#define AHB_MSB_ADDR_PTR 0xfcfc
58
59/*
60 * Register interface pages (the most significant word of the address)
61 */
62#define PAGE_IF_HW 0xd000
63#define PAGE_IF_SW 0x7000
64
65/*
66 * H/W register Interface (PAGE_IF_HW)
67 */
68#define REG_SW_LOAD_COMPLETE 0x0014
69#define REG_CMDWR_PAGE 0x0028
70#define REG_CMDWR_ADDR 0x002a
71#define REG_CMDRD_PAGE 0x002c
72#define REG_CMDRD_ADDR 0x002e
73#define REG_CMD_BUF 0x0f12
74#define REG_SET_HOST_INT 0x1000
75#define REG_CLEAR_HOST_INT 0x1030
76#define REG_PATTERN_SET 0x3100
77#define REG_PATTERN_WIDTH 0x3118
78#define REG_PATTERN_HEIGHT 0x311a
79#define REG_PATTERN_PARAM 0x311c
80
81/*
82 * S/W register interface (PAGE_IF_SW)
83 */
84
85/* Firmware revision information */
86#define REG_FW_APIVER 0x012e
87#define S5K5BAF_FW_APIVER 0x0001
88#define REG_FW_REVISION 0x0130
89#define REG_FW_SENSOR_ID 0x0152
90
91/* Initialization parameters */
92/* Master clock frequency in KHz */
93#define REG_I_INCLK_FREQ_L 0x01b8
94#define REG_I_INCLK_FREQ_H 0x01ba
95#define MIN_MCLK_FREQ_KHZ 6000U
96#define MAX_MCLK_FREQ_KHZ 48000U
97#define REG_I_USE_NPVI_CLOCKS 0x01c6
98#define NPVI_CLOCKS 1
99#define REG_I_USE_NMIPI_CLOCKS 0x01c8
100#define NMIPI_CLOCKS 1
101#define REG_I_BLOCK_INTERNAL_PLL_CALC 0x01ca
102
103/* Clock configurations, n = 0..2. REG_I_* frequency unit is 4 kHz. */
104#define REG_I_OPCLK_4KHZ(n) ((n) * 6 + 0x01cc)
105#define REG_I_MIN_OUTRATE_4KHZ(n) ((n) * 6 + 0x01ce)
106#define REG_I_MAX_OUTRATE_4KHZ(n) ((n) * 6 + 0x01d0)
107#define SCLK_PVI_FREQ 24000
108#define SCLK_MIPI_FREQ 48000
109#define PCLK_MIN_FREQ 6000
110#define PCLK_MAX_FREQ 48000
111#define REG_I_USE_REGS_API 0x01de
112#define REG_I_INIT_PARAMS_UPDATED 0x01e0
113#define REG_I_ERROR_INFO 0x01e2
114
115/* General purpose parameters */
116#define REG_USER_BRIGHTNESS 0x01e4
117#define REG_USER_CONTRAST 0x01e6
118#define REG_USER_SATURATION 0x01e8
119#define REG_USER_SHARPBLUR 0x01ea
120
121#define REG_G_SPEC_EFFECTS 0x01ee
122#define REG_G_ENABLE_PREV 0x01f0
123#define REG_G_ENABLE_PREV_CHG 0x01f2
124#define REG_G_NEW_CFG_SYNC 0x01f8
125#define REG_G_PREVREQ_IN_WIDTH 0x01fa
126#define REG_G_PREVREQ_IN_HEIGHT 0x01fc
127#define REG_G_PREVREQ_IN_XOFFS 0x01fe
128#define REG_G_PREVREQ_IN_YOFFS 0x0200
129#define REG_G_PREVZOOM_IN_WIDTH 0x020a
130#define REG_G_PREVZOOM_IN_HEIGHT 0x020c
131#define REG_G_PREVZOOM_IN_XOFFS 0x020e
132#define REG_G_PREVZOOM_IN_YOFFS 0x0210
133#define REG_G_INPUTS_CHANGE_REQ 0x021a
134#define REG_G_ACTIVE_PREV_CFG 0x021c
135#define REG_G_PREV_CFG_CHG 0x021e
136#define REG_G_PREV_OPEN_AFTER_CH 0x0220
137#define REG_G_PREV_CFG_ERROR 0x0222
138#define CFG_ERROR_RANGE 0x0b
139#define REG_G_PREV_CFG_BYPASS_CHANGED 0x022a
140#define REG_G_ACTUAL_P_FR_TIME 0x023a
141#define REG_G_ACTUAL_P_OUT_RATE 0x023c
142#define REG_G_ACTUAL_C_FR_TIME 0x023e
143#define REG_G_ACTUAL_C_OUT_RATE 0x0240
144
145/* Preview control section. n = 0...4. */
146#define PREG(n, x) ((n) * 0x26 + x)
147#define REG_P_OUT_WIDTH(n) PREG(n, 0x0242)
148#define REG_P_OUT_HEIGHT(n) PREG(n, 0x0244)
149#define REG_P_FMT(n) PREG(n, 0x0246)
150#define REG_P_MAX_OUT_RATE(n) PREG(n, 0x0248)
151#define REG_P_MIN_OUT_RATE(n) PREG(n, 0x024a)
152#define REG_P_PVI_MASK(n) PREG(n, 0x024c)
153#define PVI_MASK_MIPI 0x52
154#define REG_P_CLK_INDEX(n) PREG(n, 0x024e)
155#define CLK_PVI_INDEX 0
156#define CLK_MIPI_INDEX NPVI_CLOCKS
157#define REG_P_FR_RATE_TYPE(n) PREG(n, 0x0250)
158#define FR_RATE_DYNAMIC 0
159#define FR_RATE_FIXED 1
160#define FR_RATE_FIXED_ACCURATE 2
161#define REG_P_FR_RATE_Q_TYPE(n) PREG(n, 0x0252)
162#define FR_RATE_Q_DYNAMIC 0
163#define FR_RATE_Q_BEST_FRRATE 1 /* Binning enabled */
164#define FR_RATE_Q_BEST_QUALITY 2 /* Binning disabled */
165/* Frame period in 0.1 ms units */
166#define REG_P_MAX_FR_TIME(n) PREG(n, 0x0254)
167#define REG_P_MIN_FR_TIME(n) PREG(n, 0x0256)
168#define S5K5BAF_MIN_FR_TIME 333 /* x100 us */
169#define S5K5BAF_MAX_FR_TIME 6500 /* x100 us */
170/* The below 5 registers are for "device correction" values */
171#define REG_P_SATURATION(n) PREG(n, 0x0258)
172#define REG_P_SHARP_BLUR(n) PREG(n, 0x025a)
173#define REG_P_GLAMOUR(n) PREG(n, 0x025c)
174#define REG_P_COLORTEMP(n) PREG(n, 0x025e)
175#define REG_P_GAMMA_INDEX(n) PREG(n, 0x0260)
176#define REG_P_PREV_MIRROR(n) PREG(n, 0x0262)
177#define REG_P_CAP_MIRROR(n) PREG(n, 0x0264)
178#define REG_P_CAP_ROTATION(n) PREG(n, 0x0266)
179
180/* Extended image property controls */
181/* Exposure time in 10 us units */
182#define REG_SF_USR_EXPOSURE_L 0x03bc
183#define REG_SF_USR_EXPOSURE_H 0x03be
184#define REG_SF_USR_EXPOSURE_CHG 0x03c0
185#define REG_SF_USR_TOT_GAIN 0x03c2
186#define REG_SF_USR_TOT_GAIN_CHG 0x03c4
187#define REG_SF_RGAIN 0x03c6
188#define REG_SF_RGAIN_CHG 0x03c8
189#define REG_SF_GGAIN 0x03ca
190#define REG_SF_GGAIN_CHG 0x03cc
191#define REG_SF_BGAIN 0x03ce
192#define REG_SF_BGAIN_CHG 0x03d0
193#define REG_SF_WBGAIN_CHG 0x03d2
194#define REG_SF_FLICKER_QUANT 0x03d4
195#define REG_SF_FLICKER_QUANT_CHG 0x03d6
196
197/* Output interface (parallel/MIPI) setup */
198#define REG_OIF_EN_MIPI_LANES 0x03f2
199#define REG_OIF_EN_PACKETS 0x03f4
200#define EN_PACKETS_CSI2 0xc3
201#define REG_OIF_CFG_CHG 0x03f6
202
203/* Auto-algorithms enable mask */
204#define REG_DBG_AUTOALG_EN 0x03f8
205#define AALG_ALL_EN BIT(0)
206#define AALG_AE_EN BIT(1)
207#define AALG_DIVLEI_EN BIT(2)
208#define AALG_WB_EN BIT(3)
209#define AALG_USE_WB_FOR_ISP BIT(4)
210#define AALG_FLICKER_EN BIT(5)
211#define AALG_FIT_EN BIT(6)
212#define AALG_WRHW_EN BIT(7)
213
214/* Pointers to color correction matrices */
215#define REG_PTR_CCM_HORIZON 0x06d0
216#define REG_PTR_CCM_INCANDESCENT 0x06d4
217#define REG_PTR_CCM_WARM_WHITE 0x06d8
218#define REG_PTR_CCM_COOL_WHITE 0x06dc
219#define REG_PTR_CCM_DL50 0x06e0
220#define REG_PTR_CCM_DL65 0x06e4
221#define REG_PTR_CCM_OUTDOOR 0x06ec
222
223#define REG_ARR_CCM(n) (0x2800 + 36 * (n))
224
225static const char * const s5k5baf_supply_names[] = {
226 "vdda", /* Analog power supply 2.8V (2.6V to 3.0V) */
227 "vddreg", /* Regulator input power supply 1.8V (1.7V to 1.9V)
228 or 2.8V (2.6V to 3.0) */
229 "vddio", /* I/O power supply 1.8V (1.65V to 1.95V)
230 or 2.8V (2.5V to 3.1V) */
231};
232#define S5K5BAF_NUM_SUPPLIES ARRAY_SIZE(s5k5baf_supply_names)
233
234struct s5k5baf_gpio {
235 int gpio;
236 int level;
237};
238
239enum s5k5baf_gpio_id {
240 STBY,
241 RST,
242 NUM_GPIOS,
243};
244
245#define PAD_CIS 0
246#define PAD_OUT 1
247#define NUM_CIS_PADS 1
248#define NUM_ISP_PADS 2
249
250struct s5k5baf_pixfmt {
f5fe58fd 251 u32 code;
7d459937
AH
252 u32 colorspace;
253 /* REG_P_FMT(x) register value */
254 u16 reg_p_fmt;
255};
256
257struct s5k5baf_ctrls {
258 struct v4l2_ctrl_handler handler;
259 struct { /* Auto / manual white balance cluster */
260 struct v4l2_ctrl *awb;
261 struct v4l2_ctrl *gain_red;
262 struct v4l2_ctrl *gain_blue;
263 };
264 struct { /* Mirror cluster */
265 struct v4l2_ctrl *hflip;
266 struct v4l2_ctrl *vflip;
267 };
268 struct { /* Auto exposure / manual exposure and gain cluster */
269 struct v4l2_ctrl *auto_exp;
270 struct v4l2_ctrl *exposure;
271 struct v4l2_ctrl *gain;
272 };
273};
274
275enum {
276 S5K5BAF_FW_ID_PATCH,
277 S5K5BAF_FW_ID_CCM,
278 S5K5BAF_FW_ID_CIS,
279};
280
281struct s5k5baf_fw {
282 u16 count;
283 struct {
284 u16 id;
285 u16 offset;
286 } seq[0];
287 u16 data[0];
288};
289
290struct s5k5baf {
291 struct s5k5baf_gpio gpios[NUM_GPIOS];
292 enum v4l2_mbus_type bus_type;
293 u8 nlanes;
294 struct regulator_bulk_data supplies[S5K5BAF_NUM_SUPPLIES];
295
296 struct clk *clock;
297 u32 mclk_frequency;
298
299 struct s5k5baf_fw *fw;
300
301 struct v4l2_subdev cis_sd;
302 struct media_pad cis_pad;
303
304 struct v4l2_subdev sd;
305 struct media_pad pads[NUM_ISP_PADS];
306
307 /* protects the struct members below */
308 struct mutex lock;
309
310 int error;
311
312 struct v4l2_rect crop_sink;
313 struct v4l2_rect compose;
314 struct v4l2_rect crop_source;
315 /* index to s5k5baf_formats array */
316 int pixfmt;
317 /* actual frame interval in 100us */
318 u16 fiv;
319 /* requested frame interval in 100us */
320 u16 req_fiv;
321 /* cache for REG_DBG_AUTOALG_EN register */
322 u16 auto_alg;
323
324 struct s5k5baf_ctrls ctrls;
325
326 unsigned int streaming:1;
327 unsigned int apply_cfg:1;
328 unsigned int apply_crop:1;
329 unsigned int valid_auto_alg:1;
330 unsigned int power;
331};
332
333static const struct s5k5baf_pixfmt s5k5baf_formats[] = {
f5fe58fd 334 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 5 },
7d459937 335 /* range 16-240 */
f5fe58fd
BB
336 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_REC709, 6 },
337 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_JPEG, 0 },
7d459937
AH
338};
339
340static struct v4l2_rect s5k5baf_cis_rect = {
341 0, 0, S5K5BAF_CIS_WIDTH, S5K5BAF_CIS_HEIGHT
342};
343
344/* Setfile contains set of I2C command sequences. Each sequence has its ID.
345 * setfile format:
346 * u8 magic[4];
347 * u16 count; number of sequences
348 * struct {
349 * u16 id; sequence id
350 * u16 offset; sequence offset in data array
351 * } seq[count];
352 * u16 data[*]; array containing sequences
353 *
354 */
355static int s5k5baf_fw_parse(struct device *dev, struct s5k5baf_fw **fw,
0348bb1a 356 size_t count, const __le16 *data)
7d459937
AH
357{
358 struct s5k5baf_fw *f;
359 u16 *d, i, *end;
360 int ret;
361
362 if (count < S5K5BAG_FW_TAG_LEN + 1) {
383cc04c 363 dev_err(dev, "firmware file too short (%zu)\n", count);
7d459937
AH
364 return -EINVAL;
365 }
366
367 ret = memcmp(data, S5K5BAF_FW_TAG, S5K5BAG_FW_TAG_LEN * sizeof(u16));
368 if (ret != 0) {
369 dev_err(dev, "invalid firmware magic number\n");
370 return -EINVAL;
371 }
372
373 data += S5K5BAG_FW_TAG_LEN;
374 count -= S5K5BAG_FW_TAG_LEN;
375
376 d = devm_kzalloc(dev, count * sizeof(u16), GFP_KERNEL);
377
378 for (i = 0; i < count; ++i)
379 d[i] = le16_to_cpu(data[i]);
380
381 f = (struct s5k5baf_fw *)d;
382 if (count < 1 + 2 * f->count) {
383cc04c 383 dev_err(dev, "invalid firmware header (count=%d size=%zu)\n",
7d459937
AH
384 f->count, 2 * (count + S5K5BAG_FW_TAG_LEN));
385 return -EINVAL;
386 }
387 end = d + count;
388 d += 1 + 2 * f->count;
389
390 for (i = 0; i < f->count; ++i) {
391 if (f->seq[i].offset + d <= end)
392 continue;
393 dev_err(dev, "invalid firmware header (seq=%d)\n", i);
394 return -EINVAL;
395 }
396
397 *fw = f;
398
399 return 0;
400}
401
402static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
403{
404 return &container_of(ctrl->handler, struct s5k5baf, ctrls.handler)->sd;
405}
406
407static inline bool s5k5baf_is_cis_subdev(struct v4l2_subdev *sd)
408{
409 return sd->entity.type == MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
410}
411
412static inline struct s5k5baf *to_s5k5baf(struct v4l2_subdev *sd)
413{
414 if (s5k5baf_is_cis_subdev(sd))
415 return container_of(sd, struct s5k5baf, cis_sd);
416 else
417 return container_of(sd, struct s5k5baf, sd);
418}
419
420static u16 s5k5baf_i2c_read(struct s5k5baf *state, u16 addr)
421{
422 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
423 __be16 w, r;
0348bb1a 424 u16 res;
7d459937
AH
425 struct i2c_msg msg[] = {
426 { .addr = c->addr, .flags = 0,
427 .len = 2, .buf = (u8 *)&w },
428 { .addr = c->addr, .flags = I2C_M_RD,
429 .len = 2, .buf = (u8 *)&r },
430 };
431 int ret;
432
433 if (state->error)
434 return 0;
435
436 w = cpu_to_be16(addr);
437 ret = i2c_transfer(c->adapter, msg, 2);
0348bb1a 438 res = be16_to_cpu(r);
7d459937 439
0348bb1a 440 v4l2_dbg(3, debug, c, "i2c_read: 0x%04x : 0x%04x\n", addr, res);
7d459937
AH
441
442 if (ret != 2) {
443 v4l2_err(c, "i2c_read: error during transfer (%d)\n", ret);
444 state->error = ret;
445 }
0348bb1a 446 return res;
7d459937
AH
447}
448
449static void s5k5baf_i2c_write(struct s5k5baf *state, u16 addr, u16 val)
450{
451 u8 buf[4] = { addr >> 8, addr & 0xFF, val >> 8, val & 0xFF };
452 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
453 int ret;
454
455 if (state->error)
456 return;
457
458 ret = i2c_master_send(c, buf, 4);
459 v4l2_dbg(3, debug, c, "i2c_write: 0x%04x : 0x%04x\n", addr, val);
460
461 if (ret != 4) {
462 v4l2_err(c, "i2c_write: error during transfer (%d)\n", ret);
463 state->error = ret;
464 }
465}
466
467static u16 s5k5baf_read(struct s5k5baf *state, u16 addr)
468{
469 s5k5baf_i2c_write(state, REG_CMDRD_ADDR, addr);
470 return s5k5baf_i2c_read(state, REG_CMD_BUF);
471}
472
473static void s5k5baf_write(struct s5k5baf *state, u16 addr, u16 val)
474{
475 s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
476 s5k5baf_i2c_write(state, REG_CMD_BUF, val);
477}
478
479static void s5k5baf_write_arr_seq(struct s5k5baf *state, u16 addr,
480 u16 count, const u16 *seq)
481{
482 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
7e8f15c5 483 __be16 buf[65];
7d459937
AH
484
485 s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
486 if (state->error)
487 return;
488
7e8f15c5
AH
489 v4l2_dbg(3, debug, c, "i2c_write_seq(count=%d): %*ph\n", count,
490 min(2 * count, 64), seq);
491
7d459937 492 buf[0] = __constant_cpu_to_be16(REG_CMD_BUF);
7d459937 493
7e8f15c5
AH
494 while (count > 0) {
495 int n = min_t(int, count, ARRAY_SIZE(buf) - 1);
496 int ret, i;
7d459937 497
7e8f15c5
AH
498 for (i = 1; i <= n; ++i)
499 buf[i] = cpu_to_be16(*seq++);
500
501 i *= 2;
502 ret = i2c_master_send(c, (char *)buf, i);
503 if (ret != i) {
504 v4l2_err(c, "i2c_write_seq: error during transfer (%d)\n", ret);
505 state->error = ret;
506 break;
507 }
508
509 count -= n;
7d459937
AH
510 }
511}
512
513#define s5k5baf_write_seq(state, addr, seq...) \
514 s5k5baf_write_arr_seq(state, addr, sizeof((char[]){ seq }), \
515 (const u16 []){ seq });
516
517/* add items count at the beginning of the list */
518#define NSEQ(seq...) sizeof((char[]){ seq }), seq
519
520/*
521 * s5k5baf_write_nseq() - Writes sequences of values to sensor memory via i2c
522 * @nseq: sequence of u16 words in format:
523 * (N, address, value[1]...value[N-1])*,0
524 * Ex.:
525 * u16 seq[] = { NSEQ(0x4000, 1, 1), NSEQ(0x4010, 640, 480), 0 };
526 * ret = s5k5baf_write_nseq(c, seq);
527 */
528static void s5k5baf_write_nseq(struct s5k5baf *state, const u16 *nseq)
529{
530 int count;
531
532 while ((count = *nseq++)) {
533 u16 addr = *nseq++;
534 --count;
535
536 s5k5baf_write_arr_seq(state, addr, count, nseq);
537 nseq += count;
538 }
539}
540
541static void s5k5baf_synchronize(struct s5k5baf *state, int timeout, u16 addr)
542{
543 unsigned long end = jiffies + msecs_to_jiffies(timeout);
544 u16 reg;
545
546 s5k5baf_write(state, addr, 1);
547 do {
548 reg = s5k5baf_read(state, addr);
549 if (state->error || !reg)
550 return;
551 usleep_range(5000, 10000);
552 } while (time_is_after_jiffies(end));
553
554 v4l2_err(&state->sd, "timeout on register synchronize (%#x)\n", addr);
555 state->error = -ETIMEDOUT;
556}
557
558static u16 *s5k5baf_fw_get_seq(struct s5k5baf *state, u16 seq_id)
559{
560 struct s5k5baf_fw *fw = state->fw;
c0ee6273 561 u16 *data;
7d459937
AH
562 int i;
563
564 if (fw == NULL)
565 return NULL;
566
c0ee6273
SK
567 data = fw->data + 2 * fw->count;
568
7d459937
AH
569 for (i = 0; i < fw->count; ++i) {
570 if (fw->seq[i].id == seq_id)
571 return data + fw->seq[i].offset;
572 }
573
574 return NULL;
575}
576
577static void s5k5baf_hw_patch(struct s5k5baf *state)
578{
579 u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_PATCH);
580
581 if (seq)
582 s5k5baf_write_nseq(state, seq);
583}
584
585static void s5k5baf_hw_set_clocks(struct s5k5baf *state)
586{
587 unsigned long mclk = state->mclk_frequency / 1000;
588 u16 status;
589 static const u16 nseq_clk_cfg[] = {
590 NSEQ(REG_I_USE_NPVI_CLOCKS,
591 NPVI_CLOCKS, NMIPI_CLOCKS, 0,
592 SCLK_PVI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4,
593 SCLK_MIPI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4),
594 NSEQ(REG_I_USE_REGS_API, 1),
595 0
596 };
597
598 s5k5baf_write_seq(state, REG_I_INCLK_FREQ_L, mclk & 0xffff, mclk >> 16);
599 s5k5baf_write_nseq(state, nseq_clk_cfg);
600
601 s5k5baf_synchronize(state, 250, REG_I_INIT_PARAMS_UPDATED);
602 status = s5k5baf_read(state, REG_I_ERROR_INFO);
603 if (!state->error && status) {
604 v4l2_err(&state->sd, "error configuring PLL (%d)\n", status);
605 state->error = -EINVAL;
606 }
607}
608
609/* set custom color correction matrices for various illuminations */
610static void s5k5baf_hw_set_ccm(struct s5k5baf *state)
611{
612 u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CCM);
613
614 if (seq)
615 s5k5baf_write_nseq(state, seq);
616}
617
618/* CIS sensor tuning, based on undocumented android driver code */
619static void s5k5baf_hw_set_cis(struct s5k5baf *state)
620{
621 u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CIS);
622
623 if (!seq)
624 return;
625
626 s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_HW);
627 s5k5baf_write_nseq(state, seq);
628 s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
629}
630
631static void s5k5baf_hw_sync_cfg(struct s5k5baf *state)
632{
633 s5k5baf_write(state, REG_G_PREV_CFG_CHG, 1);
634 if (state->apply_crop) {
635 s5k5baf_write(state, REG_G_INPUTS_CHANGE_REQ, 1);
636 s5k5baf_write(state, REG_G_PREV_CFG_BYPASS_CHANGED, 1);
637 }
638 s5k5baf_synchronize(state, 500, REG_G_NEW_CFG_SYNC);
639}
640/* Set horizontal and vertical image flipping */
641static void s5k5baf_hw_set_mirror(struct s5k5baf *state)
642{
643 u16 flip = state->ctrls.vflip->val | (state->ctrls.vflip->val << 1);
644
645 s5k5baf_write(state, REG_P_PREV_MIRROR(0), flip);
646 if (state->streaming)
647 s5k5baf_hw_sync_cfg(state);
648}
649
650static void s5k5baf_hw_set_alg(struct s5k5baf *state, u16 alg, bool enable)
651{
652 u16 cur_alg, new_alg;
653
654 if (!state->valid_auto_alg)
655 cur_alg = s5k5baf_read(state, REG_DBG_AUTOALG_EN);
656 else
657 cur_alg = state->auto_alg;
658
659 new_alg = enable ? (cur_alg | alg) : (cur_alg & ~alg);
660
661 if (new_alg != cur_alg)
662 s5k5baf_write(state, REG_DBG_AUTOALG_EN, new_alg);
663
664 if (state->error)
665 return;
666
667 state->valid_auto_alg = 1;
668 state->auto_alg = new_alg;
669}
670
671/* Configure auto/manual white balance and R/G/B gains */
672static void s5k5baf_hw_set_awb(struct s5k5baf *state, int awb)
673{
674 struct s5k5baf_ctrls *ctrls = &state->ctrls;
675
676 if (!awb)
677 s5k5baf_write_seq(state, REG_SF_RGAIN,
678 ctrls->gain_red->val, 1,
679 S5K5BAF_GAIN_GREEN_DEF, 1,
680 ctrls->gain_blue->val, 1,
681 1);
682
683 s5k5baf_hw_set_alg(state, AALG_WB_EN, awb);
684}
685
686/* Program FW with exposure time, 'exposure' in us units */
687static void s5k5baf_hw_set_user_exposure(struct s5k5baf *state, int exposure)
688{
689 unsigned int time = exposure / 10;
690
691 s5k5baf_write_seq(state, REG_SF_USR_EXPOSURE_L,
692 time & 0xffff, time >> 16, 1);
693}
694
695static void s5k5baf_hw_set_user_gain(struct s5k5baf *state, int gain)
696{
697 s5k5baf_write_seq(state, REG_SF_USR_TOT_GAIN, gain, 1);
698}
699
700/* Set auto/manual exposure and total gain */
701static void s5k5baf_hw_set_auto_exposure(struct s5k5baf *state, int value)
702{
703 if (value == V4L2_EXPOSURE_AUTO) {
704 s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, true);
705 } else {
706 unsigned int exp_time = state->ctrls.exposure->val;
707
708 s5k5baf_hw_set_user_exposure(state, exp_time);
709 s5k5baf_hw_set_user_gain(state, state->ctrls.gain->val);
710 s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, false);
711 }
712}
713
714static void s5k5baf_hw_set_anti_flicker(struct s5k5baf *state, int v)
715{
716 if (v == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) {
717 s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, true);
718 } else {
719 /* The V4L2_CID_LINE_FREQUENCY control values match
720 * the register values */
721 s5k5baf_write_seq(state, REG_SF_FLICKER_QUANT, v, 1);
722 s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, false);
723 }
724}
725
726static void s5k5baf_hw_set_colorfx(struct s5k5baf *state, int val)
727{
728 static const u16 colorfx[] = {
729 [V4L2_COLORFX_NONE] = 0,
730 [V4L2_COLORFX_BW] = 1,
731 [V4L2_COLORFX_NEGATIVE] = 2,
732 [V4L2_COLORFX_SEPIA] = 3,
733 [V4L2_COLORFX_SKY_BLUE] = 4,
734 [V4L2_COLORFX_SKETCH] = 5,
735 };
736
737 s5k5baf_write(state, REG_G_SPEC_EFFECTS, colorfx[val]);
738}
739
740static int s5k5baf_find_pixfmt(struct v4l2_mbus_framefmt *mf)
741{
742 int i, c = -1;
743
744 for (i = 0; i < ARRAY_SIZE(s5k5baf_formats); i++) {
745 if (mf->colorspace != s5k5baf_formats[i].colorspace)
746 continue;
747 if (mf->code == s5k5baf_formats[i].code)
748 return i;
749 if (c < 0)
750 c = i;
751 }
752 return (c < 0) ? 0 : c;
753}
754
755static int s5k5baf_clear_error(struct s5k5baf *state)
756{
757 int ret = state->error;
758
759 state->error = 0;
760 return ret;
761}
762
763static int s5k5baf_hw_set_video_bus(struct s5k5baf *state)
764{
765 u16 en_pkts;
766
767 if (state->bus_type == V4L2_MBUS_CSI2)
768 en_pkts = EN_PACKETS_CSI2;
769 else
770 en_pkts = 0;
771
772 s5k5baf_write_seq(state, REG_OIF_EN_MIPI_LANES,
773 state->nlanes, en_pkts, 1);
774
775 return s5k5baf_clear_error(state);
776}
777
778static u16 s5k5baf_get_cfg_error(struct s5k5baf *state)
779{
780 u16 err = s5k5baf_read(state, REG_G_PREV_CFG_ERROR);
781 if (err)
782 s5k5baf_write(state, REG_G_PREV_CFG_ERROR, 0);
783 return err;
784}
785
786static void s5k5baf_hw_set_fiv(struct s5k5baf *state, u16 fiv)
787{
788 s5k5baf_write(state, REG_P_MAX_FR_TIME(0), fiv);
789 s5k5baf_hw_sync_cfg(state);
790}
791
792static void s5k5baf_hw_find_min_fiv(struct s5k5baf *state)
793{
794 u16 err, fiv;
795 int n;
796
797 fiv = s5k5baf_read(state, REG_G_ACTUAL_P_FR_TIME);
798 if (state->error)
799 return;
800
801 for (n = 5; n > 0; --n) {
802 s5k5baf_hw_set_fiv(state, fiv);
803 err = s5k5baf_get_cfg_error(state);
804 if (state->error)
805 return;
806 switch (err) {
807 case CFG_ERROR_RANGE:
808 ++fiv;
809 break;
810 case 0:
811 state->fiv = fiv;
812 v4l2_info(&state->sd,
813 "found valid frame interval: %d00us\n", fiv);
814 return;
815 default:
816 v4l2_err(&state->sd,
817 "error setting frame interval: %d\n", err);
818 state->error = -EINVAL;
819 }
bc39d69a 820 }
7d459937
AH
821 v4l2_err(&state->sd, "cannot find correct frame interval\n");
822 state->error = -ERANGE;
823}
824
825static void s5k5baf_hw_validate_cfg(struct s5k5baf *state)
826{
827 u16 err;
828
829 err = s5k5baf_get_cfg_error(state);
830 if (state->error)
831 return;
832
833 switch (err) {
834 case 0:
835 state->apply_cfg = 1;
836 return;
837 case CFG_ERROR_RANGE:
838 s5k5baf_hw_find_min_fiv(state);
839 if (!state->error)
840 state->apply_cfg = 1;
841 return;
842 default:
843 v4l2_err(&state->sd,
844 "error setting format: %d\n", err);
845 state->error = -EINVAL;
846 }
847}
848
849static void s5k5baf_rescale(struct v4l2_rect *r, const struct v4l2_rect *v,
850 const struct v4l2_rect *n,
851 const struct v4l2_rect *d)
852{
853 r->left = v->left * n->width / d->width;
854 r->top = v->top * n->height / d->height;
855 r->width = v->width * n->width / d->width;
856 r->height = v->height * n->height / d->height;
857}
858
859static int s5k5baf_hw_set_crop_rects(struct s5k5baf *state)
860{
861 struct v4l2_rect *p, r;
862 u16 err;
863 int ret;
864
865 p = &state->crop_sink;
866 s5k5baf_write_seq(state, REG_G_PREVREQ_IN_WIDTH, p->width, p->height,
867 p->left, p->top);
868
869 s5k5baf_rescale(&r, &state->crop_source, &state->crop_sink,
870 &state->compose);
871 s5k5baf_write_seq(state, REG_G_PREVZOOM_IN_WIDTH, r.width, r.height,
872 r.left, r.top);
873
874 s5k5baf_synchronize(state, 500, REG_G_INPUTS_CHANGE_REQ);
875 s5k5baf_synchronize(state, 500, REG_G_PREV_CFG_BYPASS_CHANGED);
876 err = s5k5baf_get_cfg_error(state);
877 ret = s5k5baf_clear_error(state);
878 if (ret < 0)
879 return ret;
880
881 switch (err) {
882 case 0:
883 break;
884 case CFG_ERROR_RANGE:
885 /* retry crop with frame interval set to max */
886 s5k5baf_hw_set_fiv(state, S5K5BAF_MAX_FR_TIME);
887 err = s5k5baf_get_cfg_error(state);
888 ret = s5k5baf_clear_error(state);
889 if (ret < 0)
890 return ret;
891 if (err) {
892 v4l2_err(&state->sd,
893 "crop error on max frame interval: %d\n", err);
894 state->error = -EINVAL;
895 }
896 s5k5baf_hw_set_fiv(state, state->req_fiv);
897 s5k5baf_hw_validate_cfg(state);
898 break;
899 default:
900 v4l2_err(&state->sd, "crop error: %d\n", err);
901 return -EINVAL;
902 }
903
904 if (!state->apply_cfg)
905 return 0;
906
907 p = &state->crop_source;
908 s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0), p->width, p->height);
909 s5k5baf_hw_set_fiv(state, state->req_fiv);
910 s5k5baf_hw_validate_cfg(state);
911
912 return s5k5baf_clear_error(state);
913}
914
915static void s5k5baf_hw_set_config(struct s5k5baf *state)
916{
917 u16 reg_fmt = s5k5baf_formats[state->pixfmt].reg_p_fmt;
918 struct v4l2_rect *r = &state->crop_source;
919
920 s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0),
921 r->width, r->height, reg_fmt,
922 PCLK_MAX_FREQ >> 2, PCLK_MIN_FREQ >> 2,
923 PVI_MASK_MIPI, CLK_MIPI_INDEX,
924 FR_RATE_FIXED, FR_RATE_Q_DYNAMIC,
925 state->req_fiv, S5K5BAF_MIN_FR_TIME);
926 s5k5baf_hw_sync_cfg(state);
927 s5k5baf_hw_validate_cfg(state);
928}
929
930
931static void s5k5baf_hw_set_test_pattern(struct s5k5baf *state, int id)
932{
933 s5k5baf_i2c_write(state, REG_PATTERN_WIDTH, 800);
934 s5k5baf_i2c_write(state, REG_PATTERN_HEIGHT, 511);
935 s5k5baf_i2c_write(state, REG_PATTERN_PARAM, 0);
936 s5k5baf_i2c_write(state, REG_PATTERN_SET, id);
937}
938
939static void s5k5baf_gpio_assert(struct s5k5baf *state, int id)
940{
941 struct s5k5baf_gpio *gpio = &state->gpios[id];
942
943 gpio_set_value(gpio->gpio, gpio->level);
944}
945
946static void s5k5baf_gpio_deassert(struct s5k5baf *state, int id)
947{
948 struct s5k5baf_gpio *gpio = &state->gpios[id];
949
950 gpio_set_value(gpio->gpio, !gpio->level);
951}
952
953static int s5k5baf_power_on(struct s5k5baf *state)
954{
955 int ret;
956
957 ret = regulator_bulk_enable(S5K5BAF_NUM_SUPPLIES, state->supplies);
958 if (ret < 0)
959 goto err;
960
961 ret = clk_set_rate(state->clock, state->mclk_frequency);
962 if (ret < 0)
963 goto err_reg_dis;
964
965 ret = clk_prepare_enable(state->clock);
966 if (ret < 0)
967 goto err_reg_dis;
968
969 v4l2_dbg(1, debug, &state->sd, "clock frequency: %ld\n",
970 clk_get_rate(state->clock));
971
972 s5k5baf_gpio_deassert(state, STBY);
973 usleep_range(50, 100);
974 s5k5baf_gpio_deassert(state, RST);
975 return 0;
976
977err_reg_dis:
978 regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES, state->supplies);
979err:
980 v4l2_err(&state->sd, "%s() failed (%d)\n", __func__, ret);
981 return ret;
982}
983
984static int s5k5baf_power_off(struct s5k5baf *state)
985{
986 int ret;
987
988 state->streaming = 0;
989 state->apply_cfg = 0;
990 state->apply_crop = 0;
991
992 s5k5baf_gpio_assert(state, RST);
993 s5k5baf_gpio_assert(state, STBY);
994
995 if (!IS_ERR(state->clock))
996 clk_disable_unprepare(state->clock);
997
998 ret = regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES,
999 state->supplies);
1000 if (ret < 0)
1001 v4l2_err(&state->sd, "failed to disable regulators\n");
1002
1003 return 0;
1004}
1005
1006static void s5k5baf_hw_init(struct s5k5baf *state)
1007{
1008 s5k5baf_i2c_write(state, AHB_MSB_ADDR_PTR, PAGE_IF_HW);
1009 s5k5baf_i2c_write(state, REG_CLEAR_HOST_INT, 0);
1010 s5k5baf_i2c_write(state, REG_SW_LOAD_COMPLETE, 1);
1011 s5k5baf_i2c_write(state, REG_CMDRD_PAGE, PAGE_IF_SW);
1012 s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
1013}
1014
1015/*
1016 * V4L2 subdev core and video operations
1017 */
1018
1019static void s5k5baf_initialize_data(struct s5k5baf *state)
1020{
1021 state->pixfmt = 0;
1022 state->req_fiv = 10000 / 15;
1023 state->fiv = state->req_fiv;
1024 state->valid_auto_alg = 0;
1025}
1026
1027static int s5k5baf_load_setfile(struct s5k5baf *state)
1028{
1029 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
1030 const struct firmware *fw;
1031 int ret;
1032
1033 ret = request_firmware(&fw, S5K5BAF_FW_FILENAME, &c->dev);
1034 if (ret < 0) {
1035 dev_warn(&c->dev, "firmware file (%s) not loaded\n",
1036 S5K5BAF_FW_FILENAME);
1037 return ret;
1038 }
1039
1040 ret = s5k5baf_fw_parse(&c->dev, &state->fw, fw->size / 2,
0348bb1a 1041 (__le16 *)fw->data);
7d459937
AH
1042
1043 release_firmware(fw);
1044
1045 return ret;
1046}
1047
1048static int s5k5baf_set_power(struct v4l2_subdev *sd, int on)
1049{
1050 struct s5k5baf *state = to_s5k5baf(sd);
1051 int ret = 0;
1052
1053 mutex_lock(&state->lock);
1054
1055 if (!on != state->power)
1056 goto out;
1057
1058 if (on) {
1059 if (state->fw == NULL)
1060 s5k5baf_load_setfile(state);
1061
1062 s5k5baf_initialize_data(state);
1063 ret = s5k5baf_power_on(state);
1064 if (ret < 0)
1065 goto out;
1066
1067 s5k5baf_hw_init(state);
1068 s5k5baf_hw_patch(state);
1069 s5k5baf_i2c_write(state, REG_SET_HOST_INT, 1);
1070 s5k5baf_hw_set_clocks(state);
1071
1072 ret = s5k5baf_hw_set_video_bus(state);
1073 if (ret < 0)
1074 goto out;
1075
1076 s5k5baf_hw_set_cis(state);
1077 s5k5baf_hw_set_ccm(state);
1078
1079 ret = s5k5baf_clear_error(state);
1080 if (!ret)
1081 state->power++;
1082 } else {
1083 s5k5baf_power_off(state);
1084 state->power--;
1085 }
1086
1087out:
1088 mutex_unlock(&state->lock);
1089
1090 if (!ret && on)
1091 ret = v4l2_ctrl_handler_setup(&state->ctrls.handler);
1092
1093 return ret;
1094}
1095
1096static void s5k5baf_hw_set_stream(struct s5k5baf *state, int enable)
1097{
1098 s5k5baf_write_seq(state, REG_G_ENABLE_PREV, enable, 1);
1099}
1100
1101static int s5k5baf_s_stream(struct v4l2_subdev *sd, int on)
1102{
1103 struct s5k5baf *state = to_s5k5baf(sd);
1104 int ret;
1105
1106 mutex_lock(&state->lock);
1107
1108 if (state->streaming == !!on) {
1109 ret = 0;
1110 goto out;
1111 }
1112
1113 if (on) {
1114 s5k5baf_hw_set_config(state);
1115 ret = s5k5baf_hw_set_crop_rects(state);
1116 if (ret < 0)
1117 goto out;
1118 s5k5baf_hw_set_stream(state, 1);
1119 s5k5baf_i2c_write(state, 0xb0cc, 0x000b);
1120 } else {
1121 s5k5baf_hw_set_stream(state, 0);
1122 }
1123 ret = s5k5baf_clear_error(state);
1124 if (!ret)
1125 state->streaming = !state->streaming;
1126
1127out:
1128 mutex_unlock(&state->lock);
1129
1130 return ret;
1131}
1132
1133static int s5k5baf_g_frame_interval(struct v4l2_subdev *sd,
1134 struct v4l2_subdev_frame_interval *fi)
1135{
1136 struct s5k5baf *state = to_s5k5baf(sd);
1137
1138 mutex_lock(&state->lock);
1139 fi->interval.numerator = state->fiv;
1140 fi->interval.denominator = 10000;
1141 mutex_unlock(&state->lock);
1142
1143 return 0;
1144}
1145
1146static void s5k5baf_set_frame_interval(struct s5k5baf *state,
1147 struct v4l2_subdev_frame_interval *fi)
1148{
1149 struct v4l2_fract *i = &fi->interval;
1150
1151 if (fi->interval.denominator == 0)
1152 state->req_fiv = S5K5BAF_MAX_FR_TIME;
1153 else
1154 state->req_fiv = clamp_t(u32,
1155 i->numerator * 10000 / i->denominator,
1156 S5K5BAF_MIN_FR_TIME,
1157 S5K5BAF_MAX_FR_TIME);
1158
1159 state->fiv = state->req_fiv;
1160 if (state->apply_cfg) {
1161 s5k5baf_hw_set_fiv(state, state->req_fiv);
1162 s5k5baf_hw_validate_cfg(state);
1163 }
1164 *i = (struct v4l2_fract){ state->fiv, 10000 };
1165 if (state->fiv == state->req_fiv)
1166 v4l2_info(&state->sd, "frame interval changed to %d00us\n",
1167 state->fiv);
1168}
1169
1170static int s5k5baf_s_frame_interval(struct v4l2_subdev *sd,
1171 struct v4l2_subdev_frame_interval *fi)
1172{
1173 struct s5k5baf *state = to_s5k5baf(sd);
1174
1175 mutex_lock(&state->lock);
1176 s5k5baf_set_frame_interval(state, fi);
1177 mutex_unlock(&state->lock);
1178 return 0;
1179}
1180
1181/*
1182 * V4L2 subdev pad level and video operations
1183 */
1184static int s5k5baf_enum_frame_interval(struct v4l2_subdev *sd,
f7234138 1185 struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1186 struct v4l2_subdev_frame_interval_enum *fie)
1187{
1188 if (fie->index > S5K5BAF_MAX_FR_TIME - S5K5BAF_MIN_FR_TIME ||
1189 fie->pad != PAD_CIS)
1190 return -EINVAL;
1191
1192 v4l_bound_align_image(&fie->width, S5K5BAF_WIN_WIDTH_MIN,
1193 S5K5BAF_CIS_WIDTH, 1,
1194 &fie->height, S5K5BAF_WIN_HEIGHT_MIN,
1195 S5K5BAF_CIS_HEIGHT, 1, 0);
1196
1197 fie->interval.numerator = S5K5BAF_MIN_FR_TIME + fie->index;
1198 fie->interval.denominator = 10000;
1199
1200 return 0;
1201}
1202
1203static int s5k5baf_enum_mbus_code(struct v4l2_subdev *sd,
f7234138 1204 struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1205 struct v4l2_subdev_mbus_code_enum *code)
1206{
1207 if (code->pad == PAD_CIS) {
1208 if (code->index > 0)
1209 return -EINVAL;
f5fe58fd 1210 code->code = MEDIA_BUS_FMT_FIXED;
7d459937
AH
1211 return 0;
1212 }
1213
1214 if (code->index >= ARRAY_SIZE(s5k5baf_formats))
1215 return -EINVAL;
1216
1217 code->code = s5k5baf_formats[code->index].code;
1218 return 0;
1219}
1220
1221static int s5k5baf_enum_frame_size(struct v4l2_subdev *sd,
f7234138 1222 struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1223 struct v4l2_subdev_frame_size_enum *fse)
1224{
1225 int i;
1226
1227 if (fse->index > 0)
1228 return -EINVAL;
1229
1230 if (fse->pad == PAD_CIS) {
f5fe58fd 1231 fse->code = MEDIA_BUS_FMT_FIXED;
7d459937
AH
1232 fse->min_width = S5K5BAF_CIS_WIDTH;
1233 fse->max_width = S5K5BAF_CIS_WIDTH;
1234 fse->min_height = S5K5BAF_CIS_HEIGHT;
1235 fse->max_height = S5K5BAF_CIS_HEIGHT;
1236 return 0;
1237 }
1238
1239 i = ARRAY_SIZE(s5k5baf_formats);
1240 while (--i)
1241 if (fse->code == s5k5baf_formats[i].code)
1242 break;
1243 fse->code = s5k5baf_formats[i].code;
1244 fse->min_width = S5K5BAF_WIN_WIDTH_MIN;
1245 fse->max_width = S5K5BAF_CIS_WIDTH;
1246 fse->max_height = S5K5BAF_WIN_HEIGHT_MIN;
1247 fse->min_height = S5K5BAF_CIS_HEIGHT;
1248
1249 return 0;
1250}
1251
1252static void s5k5baf_try_cis_format(struct v4l2_mbus_framefmt *mf)
1253{
1254 mf->width = S5K5BAF_CIS_WIDTH;
1255 mf->height = S5K5BAF_CIS_HEIGHT;
f5fe58fd 1256 mf->code = MEDIA_BUS_FMT_FIXED;
7d459937
AH
1257 mf->colorspace = V4L2_COLORSPACE_JPEG;
1258 mf->field = V4L2_FIELD_NONE;
1259}
1260
1261static int s5k5baf_try_isp_format(struct v4l2_mbus_framefmt *mf)
1262{
1263 int pixfmt;
1264
1265 v4l_bound_align_image(&mf->width, S5K5BAF_WIN_WIDTH_MIN,
1266 S5K5BAF_CIS_WIDTH, 1,
1267 &mf->height, S5K5BAF_WIN_HEIGHT_MIN,
1268 S5K5BAF_CIS_HEIGHT, 1, 0);
1269
1270 pixfmt = s5k5baf_find_pixfmt(mf);
1271
1272 mf->colorspace = s5k5baf_formats[pixfmt].colorspace;
1273 mf->code = s5k5baf_formats[pixfmt].code;
1274 mf->field = V4L2_FIELD_NONE;
1275
1276 return pixfmt;
1277}
1278
f7234138 1279static int s5k5baf_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1280 struct v4l2_subdev_format *fmt)
1281{
1282 struct s5k5baf *state = to_s5k5baf(sd);
1283 const struct s5k5baf_pixfmt *pixfmt;
1284 struct v4l2_mbus_framefmt *mf;
1285
1286 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
f7234138 1287 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
7d459937
AH
1288 fmt->format = *mf;
1289 return 0;
1290 }
1291
1292 mf = &fmt->format;
1293 if (fmt->pad == PAD_CIS) {
1294 s5k5baf_try_cis_format(mf);
1295 return 0;
1296 }
1297 mf->field = V4L2_FIELD_NONE;
1298 mutex_lock(&state->lock);
1299 pixfmt = &s5k5baf_formats[state->pixfmt];
1300 mf->width = state->crop_source.width;
1301 mf->height = state->crop_source.height;
1302 mf->code = pixfmt->code;
1303 mf->colorspace = pixfmt->colorspace;
1304 mutex_unlock(&state->lock);
1305
1306 return 0;
1307}
1308
f7234138 1309static int s5k5baf_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1310 struct v4l2_subdev_format *fmt)
1311{
1312 struct v4l2_mbus_framefmt *mf = &fmt->format;
1313 struct s5k5baf *state = to_s5k5baf(sd);
1314 const struct s5k5baf_pixfmt *pixfmt;
1315 int ret = 0;
1316
2f7844ec
LP
1317 mf->field = V4L2_FIELD_NONE;
1318
7d459937 1319 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
f7234138 1320 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = *mf;
7d459937
AH
1321 return 0;
1322 }
1323
1324 if (fmt->pad == PAD_CIS) {
1325 s5k5baf_try_cis_format(mf);
1326 return 0;
1327 }
1328
1329 mutex_lock(&state->lock);
1330
1331 if (state->streaming) {
1332 mutex_unlock(&state->lock);
1333 return -EBUSY;
1334 }
1335
1336 state->pixfmt = s5k5baf_try_isp_format(mf);
1337 pixfmt = &s5k5baf_formats[state->pixfmt];
1338 mf->code = pixfmt->code;
1339 mf->colorspace = pixfmt->colorspace;
1340 mf->width = state->crop_source.width;
1341 mf->height = state->crop_source.height;
1342
1343 mutex_unlock(&state->lock);
1344 return ret;
1345}
1346
1347enum selection_rect { R_CIS, R_CROP_SINK, R_COMPOSE, R_CROP_SOURCE, R_INVALID };
1348
1349static enum selection_rect s5k5baf_get_sel_rect(u32 pad, u32 target)
1350{
1351 switch (target) {
1352 case V4L2_SEL_TGT_CROP_BOUNDS:
1353 return pad ? R_COMPOSE : R_CIS;
1354 case V4L2_SEL_TGT_CROP:
1355 return pad ? R_CROP_SOURCE : R_CROP_SINK;
1356 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1357 return pad ? R_INVALID : R_CROP_SINK;
1358 case V4L2_SEL_TGT_COMPOSE:
1359 return pad ? R_INVALID : R_COMPOSE;
1360 default:
1361 return R_INVALID;
1362 }
1363}
1364
1365static int s5k5baf_is_bound_target(u32 target)
1366{
7296e158
SK
1367 return target == V4L2_SEL_TGT_CROP_BOUNDS ||
1368 target == V4L2_SEL_TGT_COMPOSE_BOUNDS;
7d459937
AH
1369}
1370
1371static int s5k5baf_get_selection(struct v4l2_subdev *sd,
f7234138 1372 struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1373 struct v4l2_subdev_selection *sel)
1374{
1375 static enum selection_rect rtype;
1376 struct s5k5baf *state = to_s5k5baf(sd);
1377
1378 rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
1379
1380 switch (rtype) {
1381 case R_INVALID:
1382 return -EINVAL;
1383 case R_CIS:
1384 sel->r = s5k5baf_cis_rect;
1385 return 0;
1386 default:
1387 break;
1388 }
1389
1390 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1391 if (rtype == R_COMPOSE)
f7234138 1392 sel->r = *v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
7d459937 1393 else
f7234138 1394 sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
7d459937
AH
1395 return 0;
1396 }
1397
1398 mutex_lock(&state->lock);
1399 switch (rtype) {
1400 case R_CROP_SINK:
1401 sel->r = state->crop_sink;
1402 break;
1403 case R_COMPOSE:
1404 sel->r = state->compose;
1405 break;
1406 case R_CROP_SOURCE:
1407 sel->r = state->crop_source;
1408 break;
1409 default:
1410 break;
1411 }
1412 if (s5k5baf_is_bound_target(sel->target)) {
1413 sel->r.left = 0;
1414 sel->r.top = 0;
1415 }
1416 mutex_unlock(&state->lock);
1417
1418 return 0;
1419}
1420
1421/* bounds range [start, start+len) to [0, max) and aligns to 2 */
1422static void s5k5baf_bound_range(u32 *start, u32 *len, u32 max)
1423{
1424 if (*len > max)
1425 *len = max;
1426 if (*start + *len > max)
1427 *start = max - *len;
1428 *start &= ~1;
1429 *len &= ~1;
1430 if (*len < S5K5BAF_WIN_WIDTH_MIN)
1431 *len = S5K5BAF_WIN_WIDTH_MIN;
1432}
1433
1434static void s5k5baf_bound_rect(struct v4l2_rect *r, u32 width, u32 height)
1435{
1436 s5k5baf_bound_range(&r->left, &r->width, width);
1437 s5k5baf_bound_range(&r->top, &r->height, height);
1438}
1439
1440static void s5k5baf_set_rect_and_adjust(struct v4l2_rect **rects,
1441 enum selection_rect first,
1442 struct v4l2_rect *v)
1443{
1444 struct v4l2_rect *r, *br;
1445 enum selection_rect i = first;
1446
1447 *rects[first] = *v;
1448 do {
1449 r = rects[i];
1450 br = rects[i - 1];
1451 s5k5baf_bound_rect(r, br->width, br->height);
1452 } while (++i != R_INVALID);
1453 *v = *rects[first];
1454}
1455
1456static bool s5k5baf_cmp_rect(const struct v4l2_rect *r1,
1457 const struct v4l2_rect *r2)
1458{
1459 return !memcmp(r1, r2, sizeof(*r1));
1460}
1461
1462static int s5k5baf_set_selection(struct v4l2_subdev *sd,
f7234138 1463 struct v4l2_subdev_pad_config *cfg,
7d459937
AH
1464 struct v4l2_subdev_selection *sel)
1465{
1466 static enum selection_rect rtype;
1467 struct s5k5baf *state = to_s5k5baf(sd);
1468 struct v4l2_rect **rects;
1469 int ret = 0;
1470
1471 rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
1472 if (rtype == R_INVALID || s5k5baf_is_bound_target(sel->target))
1473 return -EINVAL;
1474
1475 /* allow only scaling on compose */
1476 if (rtype == R_COMPOSE) {
1477 sel->r.left = 0;
1478 sel->r.top = 0;
1479 }
1480
1481 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1482 rects = (struct v4l2_rect * []) {
1483 &s5k5baf_cis_rect,
f7234138
HV
1484 v4l2_subdev_get_try_crop(sd, cfg, PAD_CIS),
1485 v4l2_subdev_get_try_compose(sd, cfg, PAD_CIS),
1486 v4l2_subdev_get_try_crop(sd, cfg, PAD_OUT)
7d459937
AH
1487 };
1488 s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
1489 return 0;
1490 }
1491
1492 rects = (struct v4l2_rect * []) {
1493 &s5k5baf_cis_rect,
1494 &state->crop_sink,
1495 &state->compose,
1496 &state->crop_source
1497 };
1498 mutex_lock(&state->lock);
1499 if (state->streaming) {
1500 /* adjust sel->r to avoid output resolution change */
1501 if (rtype < R_CROP_SOURCE) {
1502 if (sel->r.width < state->crop_source.width)
1503 sel->r.width = state->crop_source.width;
1504 if (sel->r.height < state->crop_source.height)
1505 sel->r.height = state->crop_source.height;
1506 } else {
1507 sel->r.width = state->crop_source.width;
1508 sel->r.height = state->crop_source.height;
1509 }
1510 }
1511 s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
1512 if (!s5k5baf_cmp_rect(&state->crop_sink, &s5k5baf_cis_rect) ||
1513 !s5k5baf_cmp_rect(&state->compose, &s5k5baf_cis_rect))
1514 state->apply_crop = 1;
1515 if (state->streaming)
1516 ret = s5k5baf_hw_set_crop_rects(state);
1517 mutex_unlock(&state->lock);
1518
1519 return ret;
1520}
1521
1522static const struct v4l2_subdev_pad_ops s5k5baf_cis_pad_ops = {
1523 .enum_mbus_code = s5k5baf_enum_mbus_code,
1524 .enum_frame_size = s5k5baf_enum_frame_size,
1525 .get_fmt = s5k5baf_get_fmt,
1526 .set_fmt = s5k5baf_set_fmt,
1527};
1528
1529static const struct v4l2_subdev_pad_ops s5k5baf_pad_ops = {
1530 .enum_mbus_code = s5k5baf_enum_mbus_code,
1531 .enum_frame_size = s5k5baf_enum_frame_size,
1532 .enum_frame_interval = s5k5baf_enum_frame_interval,
1533 .get_fmt = s5k5baf_get_fmt,
1534 .set_fmt = s5k5baf_set_fmt,
1535 .get_selection = s5k5baf_get_selection,
1536 .set_selection = s5k5baf_set_selection,
1537};
1538
1539static const struct v4l2_subdev_video_ops s5k5baf_video_ops = {
1540 .g_frame_interval = s5k5baf_g_frame_interval,
1541 .s_frame_interval = s5k5baf_s_frame_interval,
1542 .s_stream = s5k5baf_s_stream,
1543};
1544
1545/*
1546 * V4L2 subdev controls
1547 */
1548
1549static int s5k5baf_s_ctrl(struct v4l2_ctrl *ctrl)
1550{
1551 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1552 struct s5k5baf *state = to_s5k5baf(sd);
1553 int ret;
1554
1555 v4l2_dbg(1, debug, sd, "ctrl: %s, value: %d\n", ctrl->name, ctrl->val);
1556
1557 mutex_lock(&state->lock);
1558
1559 if (state->power == 0)
1560 goto unlock;
1561
1562 switch (ctrl->id) {
1563 case V4L2_CID_AUTO_WHITE_BALANCE:
1564 s5k5baf_hw_set_awb(state, ctrl->val);
1565 break;
1566
1567 case V4L2_CID_BRIGHTNESS:
1568 s5k5baf_write(state, REG_USER_BRIGHTNESS, ctrl->val);
1569 break;
1570
1571 case V4L2_CID_COLORFX:
1572 s5k5baf_hw_set_colorfx(state, ctrl->val);
1573 break;
1574
1575 case V4L2_CID_CONTRAST:
1576 s5k5baf_write(state, REG_USER_CONTRAST, ctrl->val);
1577 break;
1578
1579 case V4L2_CID_EXPOSURE_AUTO:
1580 s5k5baf_hw_set_auto_exposure(state, ctrl->val);
1581 break;
1582
1583 case V4L2_CID_HFLIP:
1584 s5k5baf_hw_set_mirror(state);
1585 break;
1586
1587 case V4L2_CID_POWER_LINE_FREQUENCY:
1588 s5k5baf_hw_set_anti_flicker(state, ctrl->val);
1589 break;
1590
1591 case V4L2_CID_SATURATION:
1592 s5k5baf_write(state, REG_USER_SATURATION, ctrl->val);
1593 break;
1594
1595 case V4L2_CID_SHARPNESS:
1596 s5k5baf_write(state, REG_USER_SHARPBLUR, ctrl->val);
1597 break;
1598
1599 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
1600 s5k5baf_write(state, REG_P_COLORTEMP(0), ctrl->val);
1601 if (state->apply_cfg)
1602 s5k5baf_hw_sync_cfg(state);
1603 break;
1604
1605 case V4L2_CID_TEST_PATTERN:
1606 s5k5baf_hw_set_test_pattern(state, ctrl->val);
1607 break;
1608 }
1609unlock:
1610 ret = s5k5baf_clear_error(state);
1611 mutex_unlock(&state->lock);
1612 return ret;
1613}
1614
1615static const struct v4l2_ctrl_ops s5k5baf_ctrl_ops = {
1616 .s_ctrl = s5k5baf_s_ctrl,
1617};
1618
1619static const char * const s5k5baf_test_pattern_menu[] = {
1620 "Disabled",
1621 "Blank",
1622 "Bars",
1623 "Gradients",
1624 "Textile",
1625 "Textile2",
1626 "Squares"
1627};
1628
1629static int s5k5baf_initialize_ctrls(struct s5k5baf *state)
1630{
1631 const struct v4l2_ctrl_ops *ops = &s5k5baf_ctrl_ops;
1632 struct s5k5baf_ctrls *ctrls = &state->ctrls;
1633 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1634 int ret;
1635
1636 ret = v4l2_ctrl_handler_init(hdl, 16);
1637 if (ret < 0) {
1638 v4l2_err(&state->sd, "cannot init ctrl handler (%d)\n", ret);
1639 return ret;
1640 }
1641
1642 /* Auto white balance cluster */
1643 ctrls->awb = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE,
1644 0, 1, 1, 1);
1645 ctrls->gain_red = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1646 0, 255, 1, S5K5BAF_GAIN_RED_DEF);
1647 ctrls->gain_blue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1648 0, 255, 1, S5K5BAF_GAIN_BLUE_DEF);
1649 v4l2_ctrl_auto_cluster(3, &ctrls->awb, 0, false);
1650
1651 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1652 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1653 v4l2_ctrl_cluster(2, &ctrls->hflip);
1654
1655 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1656 V4L2_CID_EXPOSURE_AUTO,
1657 V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
1658 /* Exposure time: x 1 us */
1659 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
1660 0, 6000000U, 1, 100000U);
1661 /* Total gain: 256 <=> 1x */
1662 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1663 0, 256, 1, 256);
1664 v4l2_ctrl_auto_cluster(3, &ctrls->auto_exp, 0, false);
1665
1666 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_POWER_LINE_FREQUENCY,
1667 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
1668 V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
1669
1670 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_COLORFX,
1671 V4L2_COLORFX_SKY_BLUE, ~0x6f, V4L2_COLORFX_NONE);
1672
1673 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
1674 0, 256, 1, 0);
1675
1676 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
1677 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -127, 127, 1, 0);
1678 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
1679 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -127, 127, 1, 0);
1680
1681 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
1682 ARRAY_SIZE(s5k5baf_test_pattern_menu) - 1,
1683 0, 0, s5k5baf_test_pattern_menu);
1684
1685 if (hdl->error) {
1686 v4l2_err(&state->sd, "error creating controls (%d)\n",
1687 hdl->error);
1688 ret = hdl->error;
1689 v4l2_ctrl_handler_free(hdl);
1690 return ret;
1691 }
1692
1693 state->sd.ctrl_handler = hdl;
1694 return 0;
1695}
1696
1697/*
1698 * V4L2 subdev internal operations
1699 */
1700static int s5k5baf_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1701{
1702 struct v4l2_mbus_framefmt *mf;
1703
f7234138 1704 mf = v4l2_subdev_get_try_format(sd, fh->pad, PAD_CIS);
7d459937
AH
1705 s5k5baf_try_cis_format(mf);
1706
1707 if (s5k5baf_is_cis_subdev(sd))
1708 return 0;
1709
f7234138 1710 mf = v4l2_subdev_get_try_format(sd, fh->pad, PAD_OUT);
7d459937
AH
1711 mf->colorspace = s5k5baf_formats[0].colorspace;
1712 mf->code = s5k5baf_formats[0].code;
1713 mf->width = s5k5baf_cis_rect.width;
1714 mf->height = s5k5baf_cis_rect.height;
1715 mf->field = V4L2_FIELD_NONE;
1716
f7234138
HV
1717 *v4l2_subdev_get_try_crop(sd, fh->pad, PAD_CIS) = s5k5baf_cis_rect;
1718 *v4l2_subdev_get_try_compose(sd, fh->pad, PAD_CIS) = s5k5baf_cis_rect;
1719 *v4l2_subdev_get_try_crop(sd, fh->pad, PAD_OUT) = s5k5baf_cis_rect;
7d459937
AH
1720
1721 return 0;
1722}
1723
1724static int s5k5baf_check_fw_revision(struct s5k5baf *state)
1725{
1726 u16 api_ver = 0, fw_rev = 0, s_id = 0;
1727 int ret;
1728
1729 api_ver = s5k5baf_read(state, REG_FW_APIVER);
1730 fw_rev = s5k5baf_read(state, REG_FW_REVISION) & 0xff;
1731 s_id = s5k5baf_read(state, REG_FW_SENSOR_ID);
1732 ret = s5k5baf_clear_error(state);
1733 if (ret < 0)
1734 return ret;
1735
1736 v4l2_info(&state->sd, "FW API=%#x, revision=%#x sensor_id=%#x\n",
1737 api_ver, fw_rev, s_id);
1738
1739 if (api_ver != S5K5BAF_FW_APIVER) {
1740 v4l2_err(&state->sd, "FW API version not supported\n");
1741 return -ENODEV;
1742 }
1743
1744 return 0;
1745}
1746
1747static int s5k5baf_registered(struct v4l2_subdev *sd)
1748{
1749 struct s5k5baf *state = to_s5k5baf(sd);
1750 int ret;
1751
1752 ret = v4l2_device_register_subdev(sd->v4l2_dev, &state->cis_sd);
1753 if (ret < 0)
1754 v4l2_err(sd, "failed to register subdev %s\n",
1755 state->cis_sd.name);
1756 else
1757 ret = media_entity_create_link(&state->cis_sd.entity, PAD_CIS,
1758 &state->sd.entity, PAD_CIS,
1759 MEDIA_LNK_FL_IMMUTABLE |
1760 MEDIA_LNK_FL_ENABLED);
1761 return ret;
1762}
1763
1764static void s5k5baf_unregistered(struct v4l2_subdev *sd)
1765{
1766 struct s5k5baf *state = to_s5k5baf(sd);
1767 v4l2_device_unregister_subdev(&state->cis_sd);
1768}
1769
1770static const struct v4l2_subdev_ops s5k5baf_cis_subdev_ops = {
1771 .pad = &s5k5baf_cis_pad_ops,
1772};
1773
1774static const struct v4l2_subdev_internal_ops s5k5baf_cis_subdev_internal_ops = {
1775 .open = s5k5baf_open,
1776};
1777
1778static const struct v4l2_subdev_internal_ops s5k5baf_subdev_internal_ops = {
1779 .registered = s5k5baf_registered,
1780 .unregistered = s5k5baf_unregistered,
1781 .open = s5k5baf_open,
1782};
1783
1784static const struct v4l2_subdev_core_ops s5k5baf_core_ops = {
1785 .s_power = s5k5baf_set_power,
1786 .log_status = v4l2_ctrl_subdev_log_status,
1787};
1788
1789static const struct v4l2_subdev_ops s5k5baf_subdev_ops = {
1790 .core = &s5k5baf_core_ops,
1791 .pad = &s5k5baf_pad_ops,
1792 .video = &s5k5baf_video_ops,
1793};
1794
1795static int s5k5baf_configure_gpios(struct s5k5baf *state)
1796{
0348bb1a 1797 static const char * const name[] = { "S5K5BAF_STBY", "S5K5BAF_RST" };
7d459937
AH
1798 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
1799 struct s5k5baf_gpio *g = state->gpios;
1800 int ret, i;
1801
1802 for (i = 0; i < NUM_GPIOS; ++i) {
1803 int flags = GPIOF_DIR_OUT;
1804 if (g[i].level)
1805 flags |= GPIOF_INIT_HIGH;
1806 ret = devm_gpio_request_one(&c->dev, g[i].gpio, flags, name[i]);
1807 if (ret < 0) {
1808 v4l2_err(c, "failed to request gpio %s\n", name[i]);
1809 return ret;
1810 }
1811 }
1812 return 0;
1813}
1814
1815static int s5k5baf_parse_gpios(struct s5k5baf_gpio *gpios, struct device *dev)
1816{
1817 static const char * const names[] = {
1818 "stbyn-gpios",
1819 "rstn-gpios",
1820 };
1821 struct device_node *node = dev->of_node;
1822 enum of_gpio_flags flags;
1823 int ret, i;
1824
1825 for (i = 0; i < NUM_GPIOS; ++i) {
1826 ret = of_get_named_gpio_flags(node, names[i], 0, &flags);
1827 if (ret < 0) {
1828 dev_err(dev, "no %s GPIO pin provided\n", names[i]);
1829 return ret;
1830 }
1831 gpios[i].gpio = ret;
1832 gpios[i].level = !(flags & OF_GPIO_ACTIVE_LOW);
1833 }
1834
1835 return 0;
1836}
1837
1838static int s5k5baf_parse_device_node(struct s5k5baf *state, struct device *dev)
1839{
1840 struct device_node *node = dev->of_node;
1841 struct device_node *node_ep;
1842 struct v4l2_of_endpoint ep;
1843 int ret;
1844
1845 if (!node) {
1846 dev_err(dev, "no device-tree node provided\n");
1847 return -EINVAL;
1848 }
1849
1850 ret = of_property_read_u32(node, "clock-frequency",
1851 &state->mclk_frequency);
1852 if (ret < 0) {
1853 state->mclk_frequency = S5K5BAF_DEFAULT_MCLK_FREQ;
1854 dev_info(dev, "using default %u Hz clock frequency\n",
1855 state->mclk_frequency);
1856 }
1857
1858 ret = s5k5baf_parse_gpios(state->gpios, dev);
1859 if (ret < 0)
1860 return ret;
1861
fd9fdb78 1862 node_ep = of_graph_get_next_endpoint(node, NULL);
7d459937
AH
1863 if (!node_ep) {
1864 dev_err(dev, "no endpoint defined at node %s\n",
1865 node->full_name);
1866 return -EINVAL;
1867 }
1868
1869 v4l2_of_parse_endpoint(node_ep, &ep);
1870 of_node_put(node_ep);
1871 state->bus_type = ep.bus_type;
1872
1873 switch (state->bus_type) {
1874 case V4L2_MBUS_CSI2:
1875 state->nlanes = ep.bus.mipi_csi2.num_data_lanes;
1876 break;
1877 case V4L2_MBUS_PARALLEL:
1878 break;
1879 default:
1880 dev_err(dev, "unsupported bus in endpoint defined at node %s\n",
1881 node->full_name);
1882 return -EINVAL;
1883 }
1884
1885 return 0;
1886}
1887
1888static int s5k5baf_configure_subdevs(struct s5k5baf *state,
1889 struct i2c_client *c)
1890{
1891 struct v4l2_subdev *sd;
1892 int ret;
1893
1894 sd = &state->cis_sd;
1895 v4l2_subdev_init(sd, &s5k5baf_cis_subdev_ops);
1896 sd->owner = THIS_MODULE;
1897 v4l2_set_subdevdata(sd, state);
1898 snprintf(sd->name, sizeof(sd->name), "S5K5BAF-CIS %d-%04x",
1899 i2c_adapter_id(c->adapter), c->addr);
1900
1901 sd->internal_ops = &s5k5baf_cis_subdev_internal_ops;
1902 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1903
1904 state->cis_pad.flags = MEDIA_PAD_FL_SOURCE;
1905 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
1906 ret = media_entity_init(&sd->entity, NUM_CIS_PADS, &state->cis_pad, 0);
1907 if (ret < 0)
1908 goto err;
1909
1910 sd = &state->sd;
1911 v4l2_i2c_subdev_init(sd, c, &s5k5baf_subdev_ops);
1912 snprintf(sd->name, sizeof(sd->name), "S5K5BAF-ISP %d-%04x",
1913 i2c_adapter_id(c->adapter), c->addr);
1914
1915 sd->internal_ops = &s5k5baf_subdev_internal_ops;
1916 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1917
1918 state->pads[PAD_CIS].flags = MEDIA_PAD_FL_SINK;
1919 state->pads[PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
1920 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
1921 ret = media_entity_init(&sd->entity, NUM_ISP_PADS, state->pads, 0);
1922
1923 if (!ret)
1924 return 0;
1925
1926 media_entity_cleanup(&state->cis_sd.entity);
1927err:
1928 dev_err(&c->dev, "cannot init media entity %s\n", sd->name);
1929 return ret;
1930}
1931
1932static int s5k5baf_configure_regulators(struct s5k5baf *state)
1933{
1934 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
1935 int ret;
1936 int i;
1937
1938 for (i = 0; i < S5K5BAF_NUM_SUPPLIES; i++)
1939 state->supplies[i].supply = s5k5baf_supply_names[i];
1940
1941 ret = devm_regulator_bulk_get(&c->dev, S5K5BAF_NUM_SUPPLIES,
1942 state->supplies);
1943 if (ret < 0)
1944 v4l2_err(c, "failed to get regulators\n");
1945 return ret;
1946}
1947
1948static int s5k5baf_probe(struct i2c_client *c,
1949 const struct i2c_device_id *id)
1950{
1951 struct s5k5baf *state;
1952 int ret;
1953
1954 state = devm_kzalloc(&c->dev, sizeof(*state), GFP_KERNEL);
1955 if (!state)
1956 return -ENOMEM;
1957
1958 mutex_init(&state->lock);
1959 state->crop_sink = s5k5baf_cis_rect;
1960 state->compose = s5k5baf_cis_rect;
1961 state->crop_source = s5k5baf_cis_rect;
1962
1963 ret = s5k5baf_parse_device_node(state, &c->dev);
1964 if (ret < 0)
1965 return ret;
1966
1967 ret = s5k5baf_configure_subdevs(state, c);
1968 if (ret < 0)
1969 return ret;
1970
1971 ret = s5k5baf_configure_gpios(state);
1972 if (ret < 0)
1973 goto err_me;
1974
1975 ret = s5k5baf_configure_regulators(state);
1976 if (ret < 0)
1977 goto err_me;
1978
1979 state->clock = devm_clk_get(state->sd.dev, S5K5BAF_CLK_NAME);
1980 if (IS_ERR(state->clock)) {
1981 ret = -EPROBE_DEFER;
1982 goto err_me;
1983 }
1984
1985 ret = s5k5baf_power_on(state);
1986 if (ret < 0) {
1987 ret = -EPROBE_DEFER;
1988 goto err_me;
1989 }
1990 s5k5baf_hw_init(state);
1991 ret = s5k5baf_check_fw_revision(state);
1992
1993 s5k5baf_power_off(state);
1994 if (ret < 0)
1995 goto err_me;
1996
1997 ret = s5k5baf_initialize_ctrls(state);
1998 if (ret < 0)
1999 goto err_me;
2000
2001 ret = v4l2_async_register_subdev(&state->sd);
2002 if (ret < 0)
2003 goto err_ctrl;
2004
2005 return 0;
2006
2007err_ctrl:
2008 v4l2_ctrl_handler_free(state->sd.ctrl_handler);
2009err_me:
2010 media_entity_cleanup(&state->sd.entity);
2011 media_entity_cleanup(&state->cis_sd.entity);
2012 return ret;
2013}
2014
2015static int s5k5baf_remove(struct i2c_client *c)
2016{
2017 struct v4l2_subdev *sd = i2c_get_clientdata(c);
2018 struct s5k5baf *state = to_s5k5baf(sd);
2019
2020 v4l2_async_unregister_subdev(sd);
2021 v4l2_ctrl_handler_free(sd->ctrl_handler);
2022 media_entity_cleanup(&sd->entity);
2023
2024 sd = &state->cis_sd;
2025 v4l2_device_unregister_subdev(sd);
2026 media_entity_cleanup(&sd->entity);
2027
2028 return 0;
2029}
2030
2031static const struct i2c_device_id s5k5baf_id[] = {
2032 { S5K5BAF_DRIVER_NAME, 0 },
2033 { },
2034};
2035MODULE_DEVICE_TABLE(i2c, s5k5baf_id);
2036
2037static const struct of_device_id s5k5baf_of_match[] = {
2038 { .compatible = "samsung,s5k5baf" },
2039 { }
2040};
2041MODULE_DEVICE_TABLE(of, s5k5baf_of_match);
2042
2043static struct i2c_driver s5k5baf_i2c_driver = {
2044 .driver = {
2045 .of_match_table = s5k5baf_of_match,
2046 .name = S5K5BAF_DRIVER_NAME
2047 },
2048 .probe = s5k5baf_probe,
2049 .remove = s5k5baf_remove,
2050 .id_table = s5k5baf_id,
2051};
2052
2053module_i2c_driver(s5k5baf_i2c_driver);
2054
2055MODULE_DESCRIPTION("Samsung S5K5BAF(X) UXGA camera driver");
2056MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
2057MODULE_LICENSE("GPL v2");
This page took 0.253495 seconds and 5 git commands to generate.