treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / i2c / s5k5baf.c
blobcdfe008ba39f8eebd99ee9947a3289afb461b8f9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for Samsung S5K5BAF UXGA 1/5" 2M CMOS Image Sensor
4 * with embedded SoC ISP.
6 * Copyright (C) 2013, Samsung Electronics Co., Ltd.
7 * Andrzej Hajda <a.hajda@samsung.com>
9 * Based on S5K6AA driver authored by Sylwester Nawrocki
10 * Copyright (C) 2013, Samsung Electronics Co., Ltd.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/firmware.h>
16 #include <linux/gpio.h>
17 #include <linux/i2c.h>
18 #include <linux/media.h>
19 #include <linux/module.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_graph.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
25 #include <media/media-entity.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
29 #include <media/v4l2-mediabus.h>
30 #include <media/v4l2-fwnode.h>
32 static int debug;
33 module_param(debug, int, 0644);
35 #define S5K5BAF_DRIVER_NAME "s5k5baf"
36 #define S5K5BAF_DEFAULT_MCLK_FREQ 24000000U
37 #define S5K5BAF_CLK_NAME "mclk"
39 #define S5K5BAF_FW_FILENAME "s5k5baf-cfg.bin"
40 #define S5K5BAF_FW_TAG "SF00"
41 #define S5K5BAG_FW_TAG_LEN 2
42 #define S5K5BAG_FW_MAX_COUNT 16
44 #define S5K5BAF_CIS_WIDTH 1600
45 #define S5K5BAF_CIS_HEIGHT 1200
46 #define S5K5BAF_WIN_WIDTH_MIN 8
47 #define S5K5BAF_WIN_HEIGHT_MIN 8
48 #define S5K5BAF_GAIN_RED_DEF 127
49 #define S5K5BAF_GAIN_GREEN_DEF 95
50 #define S5K5BAF_GAIN_BLUE_DEF 180
51 /* Default number of MIPI CSI-2 data lanes used */
52 #define S5K5BAF_DEF_NUM_LANES 1
54 #define AHB_MSB_ADDR_PTR 0xfcfc
57 * Register interface pages (the most significant word of the address)
59 #define PAGE_IF_HW 0xd000
60 #define PAGE_IF_SW 0x7000
63 * H/W register Interface (PAGE_IF_HW)
65 #define REG_SW_LOAD_COMPLETE 0x0014
66 #define REG_CMDWR_PAGE 0x0028
67 #define REG_CMDWR_ADDR 0x002a
68 #define REG_CMDRD_PAGE 0x002c
69 #define REG_CMDRD_ADDR 0x002e
70 #define REG_CMD_BUF 0x0f12
71 #define REG_SET_HOST_INT 0x1000
72 #define REG_CLEAR_HOST_INT 0x1030
73 #define REG_PATTERN_SET 0x3100
74 #define REG_PATTERN_WIDTH 0x3118
75 #define REG_PATTERN_HEIGHT 0x311a
76 #define REG_PATTERN_PARAM 0x311c
79 * S/W register interface (PAGE_IF_SW)
82 /* Firmware revision information */
83 #define REG_FW_APIVER 0x012e
84 #define S5K5BAF_FW_APIVER 0x0001
85 #define REG_FW_REVISION 0x0130
86 #define REG_FW_SENSOR_ID 0x0152
88 /* Initialization parameters */
89 /* Master clock frequency in KHz */
90 #define REG_I_INCLK_FREQ_L 0x01b8
91 #define REG_I_INCLK_FREQ_H 0x01ba
92 #define MIN_MCLK_FREQ_KHZ 6000U
93 #define MAX_MCLK_FREQ_KHZ 48000U
94 #define REG_I_USE_NPVI_CLOCKS 0x01c6
95 #define NPVI_CLOCKS 1
96 #define REG_I_USE_NMIPI_CLOCKS 0x01c8
97 #define NMIPI_CLOCKS 1
98 #define REG_I_BLOCK_INTERNAL_PLL_CALC 0x01ca
100 /* Clock configurations, n = 0..2. REG_I_* frequency unit is 4 kHz. */
101 #define REG_I_OPCLK_4KHZ(n) ((n) * 6 + 0x01cc)
102 #define REG_I_MIN_OUTRATE_4KHZ(n) ((n) * 6 + 0x01ce)
103 #define REG_I_MAX_OUTRATE_4KHZ(n) ((n) * 6 + 0x01d0)
104 #define SCLK_PVI_FREQ 24000
105 #define SCLK_MIPI_FREQ 48000
106 #define PCLK_MIN_FREQ 6000
107 #define PCLK_MAX_FREQ 48000
108 #define REG_I_USE_REGS_API 0x01de
109 #define REG_I_INIT_PARAMS_UPDATED 0x01e0
110 #define REG_I_ERROR_INFO 0x01e2
112 /* General purpose parameters */
113 #define REG_USER_BRIGHTNESS 0x01e4
114 #define REG_USER_CONTRAST 0x01e6
115 #define REG_USER_SATURATION 0x01e8
116 #define REG_USER_SHARPBLUR 0x01ea
118 #define REG_G_SPEC_EFFECTS 0x01ee
119 #define REG_G_ENABLE_PREV 0x01f0
120 #define REG_G_ENABLE_PREV_CHG 0x01f2
121 #define REG_G_NEW_CFG_SYNC 0x01f8
122 #define REG_G_PREVREQ_IN_WIDTH 0x01fa
123 #define REG_G_PREVREQ_IN_HEIGHT 0x01fc
124 #define REG_G_PREVREQ_IN_XOFFS 0x01fe
125 #define REG_G_PREVREQ_IN_YOFFS 0x0200
126 #define REG_G_PREVZOOM_IN_WIDTH 0x020a
127 #define REG_G_PREVZOOM_IN_HEIGHT 0x020c
128 #define REG_G_PREVZOOM_IN_XOFFS 0x020e
129 #define REG_G_PREVZOOM_IN_YOFFS 0x0210
130 #define REG_G_INPUTS_CHANGE_REQ 0x021a
131 #define REG_G_ACTIVE_PREV_CFG 0x021c
132 #define REG_G_PREV_CFG_CHG 0x021e
133 #define REG_G_PREV_OPEN_AFTER_CH 0x0220
134 #define REG_G_PREV_CFG_ERROR 0x0222
135 #define CFG_ERROR_RANGE 0x0b
136 #define REG_G_PREV_CFG_BYPASS_CHANGED 0x022a
137 #define REG_G_ACTUAL_P_FR_TIME 0x023a
138 #define REG_G_ACTUAL_P_OUT_RATE 0x023c
139 #define REG_G_ACTUAL_C_FR_TIME 0x023e
140 #define REG_G_ACTUAL_C_OUT_RATE 0x0240
142 /* Preview control section. n = 0...4. */
143 #define PREG(n, x) ((n) * 0x26 + x)
144 #define REG_P_OUT_WIDTH(n) PREG(n, 0x0242)
145 #define REG_P_OUT_HEIGHT(n) PREG(n, 0x0244)
146 #define REG_P_FMT(n) PREG(n, 0x0246)
147 #define REG_P_MAX_OUT_RATE(n) PREG(n, 0x0248)
148 #define REG_P_MIN_OUT_RATE(n) PREG(n, 0x024a)
149 #define REG_P_PVI_MASK(n) PREG(n, 0x024c)
150 #define PVI_MASK_MIPI 0x52
151 #define REG_P_CLK_INDEX(n) PREG(n, 0x024e)
152 #define CLK_PVI_INDEX 0
153 #define CLK_MIPI_INDEX NPVI_CLOCKS
154 #define REG_P_FR_RATE_TYPE(n) PREG(n, 0x0250)
155 #define FR_RATE_DYNAMIC 0
156 #define FR_RATE_FIXED 1
157 #define FR_RATE_FIXED_ACCURATE 2
158 #define REG_P_FR_RATE_Q_TYPE(n) PREG(n, 0x0252)
159 #define FR_RATE_Q_DYNAMIC 0
160 #define FR_RATE_Q_BEST_FRRATE 1 /* Binning enabled */
161 #define FR_RATE_Q_BEST_QUALITY 2 /* Binning disabled */
162 /* Frame period in 0.1 ms units */
163 #define REG_P_MAX_FR_TIME(n) PREG(n, 0x0254)
164 #define REG_P_MIN_FR_TIME(n) PREG(n, 0x0256)
165 #define S5K5BAF_MIN_FR_TIME 333 /* x100 us */
166 #define S5K5BAF_MAX_FR_TIME 6500 /* x100 us */
167 /* The below 5 registers are for "device correction" values */
168 #define REG_P_SATURATION(n) PREG(n, 0x0258)
169 #define REG_P_SHARP_BLUR(n) PREG(n, 0x025a)
170 #define REG_P_GLAMOUR(n) PREG(n, 0x025c)
171 #define REG_P_COLORTEMP(n) PREG(n, 0x025e)
172 #define REG_P_GAMMA_INDEX(n) PREG(n, 0x0260)
173 #define REG_P_PREV_MIRROR(n) PREG(n, 0x0262)
174 #define REG_P_CAP_MIRROR(n) PREG(n, 0x0264)
175 #define REG_P_CAP_ROTATION(n) PREG(n, 0x0266)
177 /* Extended image property controls */
178 /* Exposure time in 10 us units */
179 #define REG_SF_USR_EXPOSURE_L 0x03bc
180 #define REG_SF_USR_EXPOSURE_H 0x03be
181 #define REG_SF_USR_EXPOSURE_CHG 0x03c0
182 #define REG_SF_USR_TOT_GAIN 0x03c2
183 #define REG_SF_USR_TOT_GAIN_CHG 0x03c4
184 #define REG_SF_RGAIN 0x03c6
185 #define REG_SF_RGAIN_CHG 0x03c8
186 #define REG_SF_GGAIN 0x03ca
187 #define REG_SF_GGAIN_CHG 0x03cc
188 #define REG_SF_BGAIN 0x03ce
189 #define REG_SF_BGAIN_CHG 0x03d0
190 #define REG_SF_WBGAIN_CHG 0x03d2
191 #define REG_SF_FLICKER_QUANT 0x03d4
192 #define REG_SF_FLICKER_QUANT_CHG 0x03d6
194 /* Output interface (parallel/MIPI) setup */
195 #define REG_OIF_EN_MIPI_LANES 0x03f2
196 #define REG_OIF_EN_PACKETS 0x03f4
197 #define EN_PACKETS_CSI2 0xc3
198 #define REG_OIF_CFG_CHG 0x03f6
200 /* Auto-algorithms enable mask */
201 #define REG_DBG_AUTOALG_EN 0x03f8
202 #define AALG_ALL_EN BIT(0)
203 #define AALG_AE_EN BIT(1)
204 #define AALG_DIVLEI_EN BIT(2)
205 #define AALG_WB_EN BIT(3)
206 #define AALG_USE_WB_FOR_ISP BIT(4)
207 #define AALG_FLICKER_EN BIT(5)
208 #define AALG_FIT_EN BIT(6)
209 #define AALG_WRHW_EN BIT(7)
211 /* Pointers to color correction matrices */
212 #define REG_PTR_CCM_HORIZON 0x06d0
213 #define REG_PTR_CCM_INCANDESCENT 0x06d4
214 #define REG_PTR_CCM_WARM_WHITE 0x06d8
215 #define REG_PTR_CCM_COOL_WHITE 0x06dc
216 #define REG_PTR_CCM_DL50 0x06e0
217 #define REG_PTR_CCM_DL65 0x06e4
218 #define REG_PTR_CCM_OUTDOOR 0x06ec
220 #define REG_ARR_CCM(n) (0x2800 + 36 * (n))
222 static const char * const s5k5baf_supply_names[] = {
223 "vdda", /* Analog power supply 2.8V (2.6V to 3.0V) */
224 "vddreg", /* Regulator input power supply 1.8V (1.7V to 1.9V)
225 or 2.8V (2.6V to 3.0) */
226 "vddio", /* I/O power supply 1.8V (1.65V to 1.95V)
227 or 2.8V (2.5V to 3.1V) */
229 #define S5K5BAF_NUM_SUPPLIES ARRAY_SIZE(s5k5baf_supply_names)
231 struct s5k5baf_gpio {
232 int gpio;
233 int level;
236 enum s5k5baf_gpio_id {
237 STBY,
238 RST,
239 NUM_GPIOS,
242 #define PAD_CIS 0
243 #define PAD_OUT 1
244 #define NUM_CIS_PADS 1
245 #define NUM_ISP_PADS 2
247 struct s5k5baf_pixfmt {
248 u32 code;
249 u32 colorspace;
250 /* REG_P_FMT(x) register value */
251 u16 reg_p_fmt;
254 struct s5k5baf_ctrls {
255 struct v4l2_ctrl_handler handler;
256 struct { /* Auto / manual white balance cluster */
257 struct v4l2_ctrl *awb;
258 struct v4l2_ctrl *gain_red;
259 struct v4l2_ctrl *gain_blue;
261 struct { /* Mirror cluster */
262 struct v4l2_ctrl *hflip;
263 struct v4l2_ctrl *vflip;
265 struct { /* Auto exposure / manual exposure and gain cluster */
266 struct v4l2_ctrl *auto_exp;
267 struct v4l2_ctrl *exposure;
268 struct v4l2_ctrl *gain;
272 enum {
273 S5K5BAF_FW_ID_PATCH,
274 S5K5BAF_FW_ID_CCM,
275 S5K5BAF_FW_ID_CIS,
278 struct s5k5baf_fw {
279 u16 count;
280 struct {
281 u16 id;
282 u16 offset;
283 } seq[0];
284 u16 data[0];
287 struct s5k5baf {
288 struct s5k5baf_gpio gpios[NUM_GPIOS];
289 enum v4l2_mbus_type bus_type;
290 u8 nlanes;
291 struct regulator_bulk_data supplies[S5K5BAF_NUM_SUPPLIES];
293 struct clk *clock;
294 u32 mclk_frequency;
296 struct s5k5baf_fw *fw;
298 struct v4l2_subdev cis_sd;
299 struct media_pad cis_pad;
301 struct v4l2_subdev sd;
302 struct media_pad pads[NUM_ISP_PADS];
304 /* protects the struct members below */
305 struct mutex lock;
307 int error;
309 struct v4l2_rect crop_sink;
310 struct v4l2_rect compose;
311 struct v4l2_rect crop_source;
312 /* index to s5k5baf_formats array */
313 int pixfmt;
314 /* actual frame interval in 100us */
315 u16 fiv;
316 /* requested frame interval in 100us */
317 u16 req_fiv;
318 /* cache for REG_DBG_AUTOALG_EN register */
319 u16 auto_alg;
321 struct s5k5baf_ctrls ctrls;
323 unsigned int streaming:1;
324 unsigned int apply_cfg:1;
325 unsigned int apply_crop:1;
326 unsigned int valid_auto_alg:1;
327 unsigned int power;
330 static const struct s5k5baf_pixfmt s5k5baf_formats[] = {
331 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 5 },
332 /* range 16-240 */
333 { MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_REC709, 6 },
334 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_JPEG, 0 },
337 static struct v4l2_rect s5k5baf_cis_rect = {
338 0, 0, S5K5BAF_CIS_WIDTH, S5K5BAF_CIS_HEIGHT
341 /* Setfile contains set of I2C command sequences. Each sequence has its ID.
342 * setfile format:
343 * u8 magic[4];
344 * u16 count; number of sequences
345 * struct {
346 * u16 id; sequence id
347 * u16 offset; sequence offset in data array
348 * } seq[count];
349 * u16 data[*]; array containing sequences
352 static int s5k5baf_fw_parse(struct device *dev, struct s5k5baf_fw **fw,
353 size_t count, const __le16 *data)
355 struct s5k5baf_fw *f;
356 u16 *d, i, *end;
357 int ret;
359 if (count < S5K5BAG_FW_TAG_LEN + 1) {
360 dev_err(dev, "firmware file too short (%zu)\n", count);
361 return -EINVAL;
364 ret = memcmp(data, S5K5BAF_FW_TAG, S5K5BAG_FW_TAG_LEN * sizeof(u16));
365 if (ret != 0) {
366 dev_err(dev, "invalid firmware magic number\n");
367 return -EINVAL;
370 data += S5K5BAG_FW_TAG_LEN;
371 count -= S5K5BAG_FW_TAG_LEN;
373 d = devm_kcalloc(dev, count, sizeof(u16), GFP_KERNEL);
374 if (!d)
375 return -ENOMEM;
377 for (i = 0; i < count; ++i)
378 d[i] = le16_to_cpu(data[i]);
380 f = (struct s5k5baf_fw *)d;
381 if (count < 1 + 2 * f->count) {
382 dev_err(dev, "invalid firmware header (count=%d size=%zu)\n",
383 f->count, 2 * (count + S5K5BAG_FW_TAG_LEN));
384 return -EINVAL;
386 end = d + count;
387 d += 1 + 2 * f->count;
389 for (i = 0; i < f->count; ++i) {
390 if (f->seq[i].offset + d <= end)
391 continue;
392 dev_err(dev, "invalid firmware header (seq=%d)\n", i);
393 return -EINVAL;
396 *fw = f;
398 return 0;
401 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
403 return &container_of(ctrl->handler, struct s5k5baf, ctrls.handler)->sd;
406 static inline bool s5k5baf_is_cis_subdev(struct v4l2_subdev *sd)
408 return sd->entity.function == MEDIA_ENT_F_CAM_SENSOR;
411 static inline struct s5k5baf *to_s5k5baf(struct v4l2_subdev *sd)
413 if (s5k5baf_is_cis_subdev(sd))
414 return container_of(sd, struct s5k5baf, cis_sd);
415 else
416 return container_of(sd, struct s5k5baf, sd);
419 static u16 s5k5baf_i2c_read(struct s5k5baf *state, u16 addr)
421 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
422 __be16 w, r;
423 u16 res;
424 struct i2c_msg msg[] = {
425 { .addr = c->addr, .flags = 0,
426 .len = 2, .buf = (u8 *)&w },
427 { .addr = c->addr, .flags = I2C_M_RD,
428 .len = 2, .buf = (u8 *)&r },
430 int ret;
432 if (state->error)
433 return 0;
435 w = cpu_to_be16(addr);
436 ret = i2c_transfer(c->adapter, msg, 2);
437 res = be16_to_cpu(r);
439 v4l2_dbg(3, debug, c, "i2c_read: 0x%04x : 0x%04x\n", addr, res);
441 if (ret != 2) {
442 v4l2_err(c, "i2c_read: error during transfer (%d)\n", ret);
443 state->error = ret;
445 return res;
448 static void s5k5baf_i2c_write(struct s5k5baf *state, u16 addr, u16 val)
450 u8 buf[4] = { addr >> 8, addr & 0xFF, val >> 8, val & 0xFF };
451 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
452 int ret;
454 if (state->error)
455 return;
457 ret = i2c_master_send(c, buf, 4);
458 v4l2_dbg(3, debug, c, "i2c_write: 0x%04x : 0x%04x\n", addr, val);
460 if (ret != 4) {
461 v4l2_err(c, "i2c_write: error during transfer (%d)\n", ret);
462 state->error = ret;
466 static u16 s5k5baf_read(struct s5k5baf *state, u16 addr)
468 s5k5baf_i2c_write(state, REG_CMDRD_ADDR, addr);
469 return s5k5baf_i2c_read(state, REG_CMD_BUF);
472 static void s5k5baf_write(struct s5k5baf *state, u16 addr, u16 val)
474 s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
475 s5k5baf_i2c_write(state, REG_CMD_BUF, val);
478 static void s5k5baf_write_arr_seq(struct s5k5baf *state, u16 addr,
479 u16 count, const u16 *seq)
481 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
482 __be16 buf[65];
484 s5k5baf_i2c_write(state, REG_CMDWR_ADDR, addr);
485 if (state->error)
486 return;
488 v4l2_dbg(3, debug, c, "i2c_write_seq(count=%d): %*ph\n", count,
489 min(2 * count, 64), seq);
491 buf[0] = cpu_to_be16(REG_CMD_BUF);
493 while (count > 0) {
494 int n = min_t(int, count, ARRAY_SIZE(buf) - 1);
495 int ret, i;
497 for (i = 1; i <= n; ++i)
498 buf[i] = cpu_to_be16(*seq++);
500 i *= 2;
501 ret = i2c_master_send(c, (char *)buf, i);
502 if (ret != i) {
503 v4l2_err(c, "i2c_write_seq: error during transfer (%d)\n", ret);
504 state->error = ret;
505 break;
508 count -= n;
512 #define s5k5baf_write_seq(state, addr, seq...) \
513 s5k5baf_write_arr_seq(state, addr, sizeof((char[]){ seq }), \
514 (const u16 []){ seq });
516 /* add items count at the beginning of the list */
517 #define NSEQ(seq...) sizeof((char[]){ seq }), seq
520 * s5k5baf_write_nseq() - Writes sequences of values to sensor memory via i2c
521 * @nseq: sequence of u16 words in format:
522 * (N, address, value[1]...value[N-1])*,0
523 * Ex.:
524 * u16 seq[] = { NSEQ(0x4000, 1, 1), NSEQ(0x4010, 640, 480), 0 };
525 * ret = s5k5baf_write_nseq(c, seq);
527 static void s5k5baf_write_nseq(struct s5k5baf *state, const u16 *nseq)
529 int count;
531 while ((count = *nseq++)) {
532 u16 addr = *nseq++;
533 --count;
535 s5k5baf_write_arr_seq(state, addr, count, nseq);
536 nseq += count;
540 static void s5k5baf_synchronize(struct s5k5baf *state, int timeout, u16 addr)
542 unsigned long end = jiffies + msecs_to_jiffies(timeout);
543 u16 reg;
545 s5k5baf_write(state, addr, 1);
546 do {
547 reg = s5k5baf_read(state, addr);
548 if (state->error || !reg)
549 return;
550 usleep_range(5000, 10000);
551 } while (time_is_after_jiffies(end));
553 v4l2_err(&state->sd, "timeout on register synchronize (%#x)\n", addr);
554 state->error = -ETIMEDOUT;
557 static u16 *s5k5baf_fw_get_seq(struct s5k5baf *state, u16 seq_id)
559 struct s5k5baf_fw *fw = state->fw;
560 u16 *data;
561 int i;
563 if (fw == NULL)
564 return NULL;
566 data = fw->data + 2 * fw->count;
568 for (i = 0; i < fw->count; ++i) {
569 if (fw->seq[i].id == seq_id)
570 return data + fw->seq[i].offset;
573 return NULL;
576 static void s5k5baf_hw_patch(struct s5k5baf *state)
578 u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_PATCH);
580 if (seq)
581 s5k5baf_write_nseq(state, seq);
584 static void s5k5baf_hw_set_clocks(struct s5k5baf *state)
586 unsigned long mclk = state->mclk_frequency / 1000;
587 u16 status;
588 static const u16 nseq_clk_cfg[] = {
589 NSEQ(REG_I_USE_NPVI_CLOCKS,
590 NPVI_CLOCKS, NMIPI_CLOCKS, 0,
591 SCLK_PVI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4,
592 SCLK_MIPI_FREQ / 4, PCLK_MIN_FREQ / 4, PCLK_MAX_FREQ / 4),
593 NSEQ(REG_I_USE_REGS_API, 1),
597 s5k5baf_write_seq(state, REG_I_INCLK_FREQ_L, mclk & 0xffff, mclk >> 16);
598 s5k5baf_write_nseq(state, nseq_clk_cfg);
600 s5k5baf_synchronize(state, 250, REG_I_INIT_PARAMS_UPDATED);
601 status = s5k5baf_read(state, REG_I_ERROR_INFO);
602 if (!state->error && status) {
603 v4l2_err(&state->sd, "error configuring PLL (%d)\n", status);
604 state->error = -EINVAL;
608 /* set custom color correction matrices for various illuminations */
609 static void s5k5baf_hw_set_ccm(struct s5k5baf *state)
611 u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CCM);
613 if (seq)
614 s5k5baf_write_nseq(state, seq);
617 /* CIS sensor tuning, based on undocumented android driver code */
618 static void s5k5baf_hw_set_cis(struct s5k5baf *state)
620 u16 *seq = s5k5baf_fw_get_seq(state, S5K5BAF_FW_ID_CIS);
622 if (!seq)
623 return;
625 s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_HW);
626 s5k5baf_write_nseq(state, seq);
627 s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
630 static void s5k5baf_hw_sync_cfg(struct s5k5baf *state)
632 s5k5baf_write(state, REG_G_PREV_CFG_CHG, 1);
633 if (state->apply_crop) {
634 s5k5baf_write(state, REG_G_INPUTS_CHANGE_REQ, 1);
635 s5k5baf_write(state, REG_G_PREV_CFG_BYPASS_CHANGED, 1);
637 s5k5baf_synchronize(state, 500, REG_G_NEW_CFG_SYNC);
639 /* Set horizontal and vertical image flipping */
640 static void s5k5baf_hw_set_mirror(struct s5k5baf *state)
642 u16 flip = state->ctrls.vflip->val | (state->ctrls.vflip->val << 1);
644 s5k5baf_write(state, REG_P_PREV_MIRROR(0), flip);
645 if (state->streaming)
646 s5k5baf_hw_sync_cfg(state);
649 static void s5k5baf_hw_set_alg(struct s5k5baf *state, u16 alg, bool enable)
651 u16 cur_alg, new_alg;
653 if (!state->valid_auto_alg)
654 cur_alg = s5k5baf_read(state, REG_DBG_AUTOALG_EN);
655 else
656 cur_alg = state->auto_alg;
658 new_alg = enable ? (cur_alg | alg) : (cur_alg & ~alg);
660 if (new_alg != cur_alg)
661 s5k5baf_write(state, REG_DBG_AUTOALG_EN, new_alg);
663 if (state->error)
664 return;
666 state->valid_auto_alg = 1;
667 state->auto_alg = new_alg;
670 /* Configure auto/manual white balance and R/G/B gains */
671 static void s5k5baf_hw_set_awb(struct s5k5baf *state, int awb)
673 struct s5k5baf_ctrls *ctrls = &state->ctrls;
675 if (!awb)
676 s5k5baf_write_seq(state, REG_SF_RGAIN,
677 ctrls->gain_red->val, 1,
678 S5K5BAF_GAIN_GREEN_DEF, 1,
679 ctrls->gain_blue->val, 1,
682 s5k5baf_hw_set_alg(state, AALG_WB_EN, awb);
685 /* Program FW with exposure time, 'exposure' in us units */
686 static void s5k5baf_hw_set_user_exposure(struct s5k5baf *state, int exposure)
688 unsigned int time = exposure / 10;
690 s5k5baf_write_seq(state, REG_SF_USR_EXPOSURE_L,
691 time & 0xffff, time >> 16, 1);
694 static void s5k5baf_hw_set_user_gain(struct s5k5baf *state, int gain)
696 s5k5baf_write_seq(state, REG_SF_USR_TOT_GAIN, gain, 1);
699 /* Set auto/manual exposure and total gain */
700 static void s5k5baf_hw_set_auto_exposure(struct s5k5baf *state, int value)
702 if (value == V4L2_EXPOSURE_AUTO) {
703 s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, true);
704 } else {
705 unsigned int exp_time = state->ctrls.exposure->val;
707 s5k5baf_hw_set_user_exposure(state, exp_time);
708 s5k5baf_hw_set_user_gain(state, state->ctrls.gain->val);
709 s5k5baf_hw_set_alg(state, AALG_AE_EN | AALG_DIVLEI_EN, false);
713 static void s5k5baf_hw_set_anti_flicker(struct s5k5baf *state, int v)
715 if (v == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) {
716 s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, true);
717 } else {
718 /* The V4L2_CID_LINE_FREQUENCY control values match
719 * the register values */
720 s5k5baf_write_seq(state, REG_SF_FLICKER_QUANT, v, 1);
721 s5k5baf_hw_set_alg(state, AALG_FLICKER_EN, false);
725 static void s5k5baf_hw_set_colorfx(struct s5k5baf *state, int val)
727 static const u16 colorfx[] = {
728 [V4L2_COLORFX_NONE] = 0,
729 [V4L2_COLORFX_BW] = 1,
730 [V4L2_COLORFX_NEGATIVE] = 2,
731 [V4L2_COLORFX_SEPIA] = 3,
732 [V4L2_COLORFX_SKY_BLUE] = 4,
733 [V4L2_COLORFX_SKETCH] = 5,
736 s5k5baf_write(state, REG_G_SPEC_EFFECTS, colorfx[val]);
739 static int s5k5baf_find_pixfmt(struct v4l2_mbus_framefmt *mf)
741 int i, c = -1;
743 for (i = 0; i < ARRAY_SIZE(s5k5baf_formats); i++) {
744 if (mf->colorspace != s5k5baf_formats[i].colorspace)
745 continue;
746 if (mf->code == s5k5baf_formats[i].code)
747 return i;
748 if (c < 0)
749 c = i;
751 return (c < 0) ? 0 : c;
754 static int s5k5baf_clear_error(struct s5k5baf *state)
756 int ret = state->error;
758 state->error = 0;
759 return ret;
762 static int s5k5baf_hw_set_video_bus(struct s5k5baf *state)
764 u16 en_pkts;
766 if (state->bus_type == V4L2_MBUS_CSI2_DPHY)
767 en_pkts = EN_PACKETS_CSI2;
768 else
769 en_pkts = 0;
771 s5k5baf_write_seq(state, REG_OIF_EN_MIPI_LANES,
772 state->nlanes, en_pkts, 1);
774 return s5k5baf_clear_error(state);
777 static u16 s5k5baf_get_cfg_error(struct s5k5baf *state)
779 u16 err = s5k5baf_read(state, REG_G_PREV_CFG_ERROR);
780 if (err)
781 s5k5baf_write(state, REG_G_PREV_CFG_ERROR, 0);
782 return err;
785 static void s5k5baf_hw_set_fiv(struct s5k5baf *state, u16 fiv)
787 s5k5baf_write(state, REG_P_MAX_FR_TIME(0), fiv);
788 s5k5baf_hw_sync_cfg(state);
791 static void s5k5baf_hw_find_min_fiv(struct s5k5baf *state)
793 u16 err, fiv;
794 int n;
796 fiv = s5k5baf_read(state, REG_G_ACTUAL_P_FR_TIME);
797 if (state->error)
798 return;
800 for (n = 5; n > 0; --n) {
801 s5k5baf_hw_set_fiv(state, fiv);
802 err = s5k5baf_get_cfg_error(state);
803 if (state->error)
804 return;
805 switch (err) {
806 case CFG_ERROR_RANGE:
807 ++fiv;
808 break;
809 case 0:
810 state->fiv = fiv;
811 v4l2_info(&state->sd,
812 "found valid frame interval: %d00us\n", fiv);
813 return;
814 default:
815 v4l2_err(&state->sd,
816 "error setting frame interval: %d\n", err);
817 state->error = -EINVAL;
820 v4l2_err(&state->sd, "cannot find correct frame interval\n");
821 state->error = -ERANGE;
824 static void s5k5baf_hw_validate_cfg(struct s5k5baf *state)
826 u16 err;
828 err = s5k5baf_get_cfg_error(state);
829 if (state->error)
830 return;
832 switch (err) {
833 case 0:
834 state->apply_cfg = 1;
835 return;
836 case CFG_ERROR_RANGE:
837 s5k5baf_hw_find_min_fiv(state);
838 if (!state->error)
839 state->apply_cfg = 1;
840 return;
841 default:
842 v4l2_err(&state->sd,
843 "error setting format: %d\n", err);
844 state->error = -EINVAL;
848 static void s5k5baf_rescale(struct v4l2_rect *r, const struct v4l2_rect *v,
849 const struct v4l2_rect *n,
850 const struct v4l2_rect *d)
852 r->left = v->left * n->width / d->width;
853 r->top = v->top * n->height / d->height;
854 r->width = v->width * n->width / d->width;
855 r->height = v->height * n->height / d->height;
858 static int s5k5baf_hw_set_crop_rects(struct s5k5baf *state)
860 struct v4l2_rect *p, r;
861 u16 err;
862 int ret;
864 p = &state->crop_sink;
865 s5k5baf_write_seq(state, REG_G_PREVREQ_IN_WIDTH, p->width, p->height,
866 p->left, p->top);
868 s5k5baf_rescale(&r, &state->crop_source, &state->crop_sink,
869 &state->compose);
870 s5k5baf_write_seq(state, REG_G_PREVZOOM_IN_WIDTH, r.width, r.height,
871 r.left, r.top);
873 s5k5baf_synchronize(state, 500, REG_G_INPUTS_CHANGE_REQ);
874 s5k5baf_synchronize(state, 500, REG_G_PREV_CFG_BYPASS_CHANGED);
875 err = s5k5baf_get_cfg_error(state);
876 ret = s5k5baf_clear_error(state);
877 if (ret < 0)
878 return ret;
880 switch (err) {
881 case 0:
882 break;
883 case CFG_ERROR_RANGE:
884 /* retry crop with frame interval set to max */
885 s5k5baf_hw_set_fiv(state, S5K5BAF_MAX_FR_TIME);
886 err = s5k5baf_get_cfg_error(state);
887 ret = s5k5baf_clear_error(state);
888 if (ret < 0)
889 return ret;
890 if (err) {
891 v4l2_err(&state->sd,
892 "crop error on max frame interval: %d\n", err);
893 state->error = -EINVAL;
895 s5k5baf_hw_set_fiv(state, state->req_fiv);
896 s5k5baf_hw_validate_cfg(state);
897 break;
898 default:
899 v4l2_err(&state->sd, "crop error: %d\n", err);
900 return -EINVAL;
903 if (!state->apply_cfg)
904 return 0;
906 p = &state->crop_source;
907 s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0), p->width, p->height);
908 s5k5baf_hw_set_fiv(state, state->req_fiv);
909 s5k5baf_hw_validate_cfg(state);
911 return s5k5baf_clear_error(state);
914 static void s5k5baf_hw_set_config(struct s5k5baf *state)
916 u16 reg_fmt = s5k5baf_formats[state->pixfmt].reg_p_fmt;
917 struct v4l2_rect *r = &state->crop_source;
919 s5k5baf_write_seq(state, REG_P_OUT_WIDTH(0),
920 r->width, r->height, reg_fmt,
921 PCLK_MAX_FREQ >> 2, PCLK_MIN_FREQ >> 2,
922 PVI_MASK_MIPI, CLK_MIPI_INDEX,
923 FR_RATE_FIXED, FR_RATE_Q_DYNAMIC,
924 state->req_fiv, S5K5BAF_MIN_FR_TIME);
925 s5k5baf_hw_sync_cfg(state);
926 s5k5baf_hw_validate_cfg(state);
930 static void s5k5baf_hw_set_test_pattern(struct s5k5baf *state, int id)
932 s5k5baf_i2c_write(state, REG_PATTERN_WIDTH, 800);
933 s5k5baf_i2c_write(state, REG_PATTERN_HEIGHT, 511);
934 s5k5baf_i2c_write(state, REG_PATTERN_PARAM, 0);
935 s5k5baf_i2c_write(state, REG_PATTERN_SET, id);
938 static void s5k5baf_gpio_assert(struct s5k5baf *state, int id)
940 struct s5k5baf_gpio *gpio = &state->gpios[id];
942 gpio_set_value(gpio->gpio, gpio->level);
945 static void s5k5baf_gpio_deassert(struct s5k5baf *state, int id)
947 struct s5k5baf_gpio *gpio = &state->gpios[id];
949 gpio_set_value(gpio->gpio, !gpio->level);
952 static int s5k5baf_power_on(struct s5k5baf *state)
954 int ret;
956 ret = regulator_bulk_enable(S5K5BAF_NUM_SUPPLIES, state->supplies);
957 if (ret < 0)
958 goto err;
960 ret = clk_set_rate(state->clock, state->mclk_frequency);
961 if (ret < 0)
962 goto err_reg_dis;
964 ret = clk_prepare_enable(state->clock);
965 if (ret < 0)
966 goto err_reg_dis;
968 v4l2_dbg(1, debug, &state->sd, "clock frequency: %ld\n",
969 clk_get_rate(state->clock));
971 s5k5baf_gpio_deassert(state, STBY);
972 usleep_range(50, 100);
973 s5k5baf_gpio_deassert(state, RST);
974 return 0;
976 err_reg_dis:
977 regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES, state->supplies);
978 err:
979 v4l2_err(&state->sd, "%s() failed (%d)\n", __func__, ret);
980 return ret;
983 static int s5k5baf_power_off(struct s5k5baf *state)
985 int ret;
987 state->streaming = 0;
988 state->apply_cfg = 0;
989 state->apply_crop = 0;
991 s5k5baf_gpio_assert(state, RST);
992 s5k5baf_gpio_assert(state, STBY);
994 if (!IS_ERR(state->clock))
995 clk_disable_unprepare(state->clock);
997 ret = regulator_bulk_disable(S5K5BAF_NUM_SUPPLIES,
998 state->supplies);
999 if (ret < 0)
1000 v4l2_err(&state->sd, "failed to disable regulators\n");
1002 return 0;
1005 static void s5k5baf_hw_init(struct s5k5baf *state)
1007 s5k5baf_i2c_write(state, AHB_MSB_ADDR_PTR, PAGE_IF_HW);
1008 s5k5baf_i2c_write(state, REG_CLEAR_HOST_INT, 0);
1009 s5k5baf_i2c_write(state, REG_SW_LOAD_COMPLETE, 1);
1010 s5k5baf_i2c_write(state, REG_CMDRD_PAGE, PAGE_IF_SW);
1011 s5k5baf_i2c_write(state, REG_CMDWR_PAGE, PAGE_IF_SW);
1015 * V4L2 subdev core and video operations
1018 static void s5k5baf_initialize_data(struct s5k5baf *state)
1020 state->pixfmt = 0;
1021 state->req_fiv = 10000 / 15;
1022 state->fiv = state->req_fiv;
1023 state->valid_auto_alg = 0;
1026 static int s5k5baf_load_setfile(struct s5k5baf *state)
1028 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
1029 const struct firmware *fw;
1030 int ret;
1032 ret = request_firmware(&fw, S5K5BAF_FW_FILENAME, &c->dev);
1033 if (ret < 0) {
1034 dev_warn(&c->dev, "firmware file (%s) not loaded\n",
1035 S5K5BAF_FW_FILENAME);
1036 return ret;
1039 ret = s5k5baf_fw_parse(&c->dev, &state->fw, fw->size / 2,
1040 (__le16 *)fw->data);
1042 release_firmware(fw);
1044 return ret;
1047 static int s5k5baf_set_power(struct v4l2_subdev *sd, int on)
1049 struct s5k5baf *state = to_s5k5baf(sd);
1050 int ret = 0;
1052 mutex_lock(&state->lock);
1054 if (state->power != !on)
1055 goto out;
1057 if (on) {
1058 if (state->fw == NULL)
1059 s5k5baf_load_setfile(state);
1061 s5k5baf_initialize_data(state);
1062 ret = s5k5baf_power_on(state);
1063 if (ret < 0)
1064 goto out;
1066 s5k5baf_hw_init(state);
1067 s5k5baf_hw_patch(state);
1068 s5k5baf_i2c_write(state, REG_SET_HOST_INT, 1);
1069 s5k5baf_hw_set_clocks(state);
1071 ret = s5k5baf_hw_set_video_bus(state);
1072 if (ret < 0)
1073 goto out;
1075 s5k5baf_hw_set_cis(state);
1076 s5k5baf_hw_set_ccm(state);
1078 ret = s5k5baf_clear_error(state);
1079 if (!ret)
1080 state->power++;
1081 } else {
1082 s5k5baf_power_off(state);
1083 state->power--;
1086 out:
1087 mutex_unlock(&state->lock);
1089 if (!ret && on)
1090 ret = v4l2_ctrl_handler_setup(&state->ctrls.handler);
1092 return ret;
1095 static void s5k5baf_hw_set_stream(struct s5k5baf *state, int enable)
1097 s5k5baf_write_seq(state, REG_G_ENABLE_PREV, enable, 1);
1100 static int s5k5baf_s_stream(struct v4l2_subdev *sd, int on)
1102 struct s5k5baf *state = to_s5k5baf(sd);
1103 int ret;
1105 mutex_lock(&state->lock);
1107 if (state->streaming == !!on) {
1108 ret = 0;
1109 goto out;
1112 if (on) {
1113 s5k5baf_hw_set_config(state);
1114 ret = s5k5baf_hw_set_crop_rects(state);
1115 if (ret < 0)
1116 goto out;
1117 s5k5baf_hw_set_stream(state, 1);
1118 s5k5baf_i2c_write(state, 0xb0cc, 0x000b);
1119 } else {
1120 s5k5baf_hw_set_stream(state, 0);
1122 ret = s5k5baf_clear_error(state);
1123 if (!ret)
1124 state->streaming = !state->streaming;
1126 out:
1127 mutex_unlock(&state->lock);
1129 return ret;
1132 static int s5k5baf_g_frame_interval(struct v4l2_subdev *sd,
1133 struct v4l2_subdev_frame_interval *fi)
1135 struct s5k5baf *state = to_s5k5baf(sd);
1137 mutex_lock(&state->lock);
1138 fi->interval.numerator = state->fiv;
1139 fi->interval.denominator = 10000;
1140 mutex_unlock(&state->lock);
1142 return 0;
1145 static void s5k5baf_set_frame_interval(struct s5k5baf *state,
1146 struct v4l2_subdev_frame_interval *fi)
1148 struct v4l2_fract *i = &fi->interval;
1150 if (fi->interval.denominator == 0)
1151 state->req_fiv = S5K5BAF_MAX_FR_TIME;
1152 else
1153 state->req_fiv = clamp_t(u32,
1154 i->numerator * 10000 / i->denominator,
1155 S5K5BAF_MIN_FR_TIME,
1156 S5K5BAF_MAX_FR_TIME);
1158 state->fiv = state->req_fiv;
1159 if (state->apply_cfg) {
1160 s5k5baf_hw_set_fiv(state, state->req_fiv);
1161 s5k5baf_hw_validate_cfg(state);
1163 *i = (struct v4l2_fract){ state->fiv, 10000 };
1164 if (state->fiv == state->req_fiv)
1165 v4l2_info(&state->sd, "frame interval changed to %d00us\n",
1166 state->fiv);
1169 static int s5k5baf_s_frame_interval(struct v4l2_subdev *sd,
1170 struct v4l2_subdev_frame_interval *fi)
1172 struct s5k5baf *state = to_s5k5baf(sd);
1174 mutex_lock(&state->lock);
1175 s5k5baf_set_frame_interval(state, fi);
1176 mutex_unlock(&state->lock);
1177 return 0;
1181 * V4L2 subdev pad level and video operations
1183 static int s5k5baf_enum_frame_interval(struct v4l2_subdev *sd,
1184 struct v4l2_subdev_pad_config *cfg,
1185 struct v4l2_subdev_frame_interval_enum *fie)
1187 if (fie->index > S5K5BAF_MAX_FR_TIME - S5K5BAF_MIN_FR_TIME ||
1188 fie->pad != PAD_CIS)
1189 return -EINVAL;
1191 v4l_bound_align_image(&fie->width, S5K5BAF_WIN_WIDTH_MIN,
1192 S5K5BAF_CIS_WIDTH, 1,
1193 &fie->height, S5K5BAF_WIN_HEIGHT_MIN,
1194 S5K5BAF_CIS_HEIGHT, 1, 0);
1196 fie->interval.numerator = S5K5BAF_MIN_FR_TIME + fie->index;
1197 fie->interval.denominator = 10000;
1199 return 0;
1202 static int s5k5baf_enum_mbus_code(struct v4l2_subdev *sd,
1203 struct v4l2_subdev_pad_config *cfg,
1204 struct v4l2_subdev_mbus_code_enum *code)
1206 if (code->pad == PAD_CIS) {
1207 if (code->index > 0)
1208 return -EINVAL;
1209 code->code = MEDIA_BUS_FMT_FIXED;
1210 return 0;
1213 if (code->index >= ARRAY_SIZE(s5k5baf_formats))
1214 return -EINVAL;
1216 code->code = s5k5baf_formats[code->index].code;
1217 return 0;
1220 static int s5k5baf_enum_frame_size(struct v4l2_subdev *sd,
1221 struct v4l2_subdev_pad_config *cfg,
1222 struct v4l2_subdev_frame_size_enum *fse)
1224 int i;
1226 if (fse->index > 0)
1227 return -EINVAL;
1229 if (fse->pad == PAD_CIS) {
1230 fse->code = MEDIA_BUS_FMT_FIXED;
1231 fse->min_width = S5K5BAF_CIS_WIDTH;
1232 fse->max_width = S5K5BAF_CIS_WIDTH;
1233 fse->min_height = S5K5BAF_CIS_HEIGHT;
1234 fse->max_height = S5K5BAF_CIS_HEIGHT;
1235 return 0;
1238 i = ARRAY_SIZE(s5k5baf_formats);
1239 while (--i)
1240 if (fse->code == s5k5baf_formats[i].code)
1241 break;
1242 fse->code = s5k5baf_formats[i].code;
1243 fse->min_width = S5K5BAF_WIN_WIDTH_MIN;
1244 fse->max_width = S5K5BAF_CIS_WIDTH;
1245 fse->max_height = S5K5BAF_WIN_HEIGHT_MIN;
1246 fse->min_height = S5K5BAF_CIS_HEIGHT;
1248 return 0;
1251 static void s5k5baf_try_cis_format(struct v4l2_mbus_framefmt *mf)
1253 mf->width = S5K5BAF_CIS_WIDTH;
1254 mf->height = S5K5BAF_CIS_HEIGHT;
1255 mf->code = MEDIA_BUS_FMT_FIXED;
1256 mf->colorspace = V4L2_COLORSPACE_JPEG;
1257 mf->field = V4L2_FIELD_NONE;
1260 static int s5k5baf_try_isp_format(struct v4l2_mbus_framefmt *mf)
1262 int pixfmt;
1264 v4l_bound_align_image(&mf->width, S5K5BAF_WIN_WIDTH_MIN,
1265 S5K5BAF_CIS_WIDTH, 1,
1266 &mf->height, S5K5BAF_WIN_HEIGHT_MIN,
1267 S5K5BAF_CIS_HEIGHT, 1, 0);
1269 pixfmt = s5k5baf_find_pixfmt(mf);
1271 mf->colorspace = s5k5baf_formats[pixfmt].colorspace;
1272 mf->code = s5k5baf_formats[pixfmt].code;
1273 mf->field = V4L2_FIELD_NONE;
1275 return pixfmt;
1278 static int s5k5baf_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1279 struct v4l2_subdev_format *fmt)
1281 struct s5k5baf *state = to_s5k5baf(sd);
1282 const struct s5k5baf_pixfmt *pixfmt;
1283 struct v4l2_mbus_framefmt *mf;
1285 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1286 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1287 fmt->format = *mf;
1288 return 0;
1291 mf = &fmt->format;
1292 if (fmt->pad == PAD_CIS) {
1293 s5k5baf_try_cis_format(mf);
1294 return 0;
1296 mf->field = V4L2_FIELD_NONE;
1297 mutex_lock(&state->lock);
1298 pixfmt = &s5k5baf_formats[state->pixfmt];
1299 mf->width = state->crop_source.width;
1300 mf->height = state->crop_source.height;
1301 mf->code = pixfmt->code;
1302 mf->colorspace = pixfmt->colorspace;
1303 mutex_unlock(&state->lock);
1305 return 0;
1308 static int s5k5baf_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
1309 struct v4l2_subdev_format *fmt)
1311 struct v4l2_mbus_framefmt *mf = &fmt->format;
1312 struct s5k5baf *state = to_s5k5baf(sd);
1313 const struct s5k5baf_pixfmt *pixfmt;
1314 int ret = 0;
1316 mf->field = V4L2_FIELD_NONE;
1318 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1319 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = *mf;
1320 return 0;
1323 if (fmt->pad == PAD_CIS) {
1324 s5k5baf_try_cis_format(mf);
1325 return 0;
1328 mutex_lock(&state->lock);
1330 if (state->streaming) {
1331 mutex_unlock(&state->lock);
1332 return -EBUSY;
1335 state->pixfmt = s5k5baf_try_isp_format(mf);
1336 pixfmt = &s5k5baf_formats[state->pixfmt];
1337 mf->code = pixfmt->code;
1338 mf->colorspace = pixfmt->colorspace;
1339 mf->width = state->crop_source.width;
1340 mf->height = state->crop_source.height;
1342 mutex_unlock(&state->lock);
1343 return ret;
1346 enum selection_rect { R_CIS, R_CROP_SINK, R_COMPOSE, R_CROP_SOURCE, R_INVALID };
1348 static enum selection_rect s5k5baf_get_sel_rect(u32 pad, u32 target)
1350 switch (target) {
1351 case V4L2_SEL_TGT_CROP_BOUNDS:
1352 return pad ? R_COMPOSE : R_CIS;
1353 case V4L2_SEL_TGT_CROP:
1354 return pad ? R_CROP_SOURCE : R_CROP_SINK;
1355 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1356 return pad ? R_INVALID : R_CROP_SINK;
1357 case V4L2_SEL_TGT_COMPOSE:
1358 return pad ? R_INVALID : R_COMPOSE;
1359 default:
1360 return R_INVALID;
1364 static int s5k5baf_is_bound_target(u32 target)
1366 return target == V4L2_SEL_TGT_CROP_BOUNDS ||
1367 target == V4L2_SEL_TGT_COMPOSE_BOUNDS;
1370 static int s5k5baf_get_selection(struct v4l2_subdev *sd,
1371 struct v4l2_subdev_pad_config *cfg,
1372 struct v4l2_subdev_selection *sel)
1374 enum selection_rect rtype;
1375 struct s5k5baf *state = to_s5k5baf(sd);
1377 rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
1379 switch (rtype) {
1380 case R_INVALID:
1381 return -EINVAL;
1382 case R_CIS:
1383 sel->r = s5k5baf_cis_rect;
1384 return 0;
1385 default:
1386 break;
1389 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1390 if (rtype == R_COMPOSE)
1391 sel->r = *v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
1392 else
1393 sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1394 return 0;
1397 mutex_lock(&state->lock);
1398 switch (rtype) {
1399 case R_CROP_SINK:
1400 sel->r = state->crop_sink;
1401 break;
1402 case R_COMPOSE:
1403 sel->r = state->compose;
1404 break;
1405 case R_CROP_SOURCE:
1406 sel->r = state->crop_source;
1407 break;
1408 default:
1409 break;
1411 if (s5k5baf_is_bound_target(sel->target)) {
1412 sel->r.left = 0;
1413 sel->r.top = 0;
1415 mutex_unlock(&state->lock);
1417 return 0;
1420 /* bounds range [start, start+len) to [0, max) and aligns to 2 */
1421 static void s5k5baf_bound_range(u32 *start, u32 *len, u32 max)
1423 if (*len > max)
1424 *len = max;
1425 if (*start + *len > max)
1426 *start = max - *len;
1427 *start &= ~1;
1428 *len &= ~1;
1429 if (*len < S5K5BAF_WIN_WIDTH_MIN)
1430 *len = S5K5BAF_WIN_WIDTH_MIN;
1433 static void s5k5baf_bound_rect(struct v4l2_rect *r, u32 width, u32 height)
1435 s5k5baf_bound_range(&r->left, &r->width, width);
1436 s5k5baf_bound_range(&r->top, &r->height, height);
1439 static void s5k5baf_set_rect_and_adjust(struct v4l2_rect **rects,
1440 enum selection_rect first,
1441 struct v4l2_rect *v)
1443 struct v4l2_rect *r, *br;
1444 enum selection_rect i = first;
1446 *rects[first] = *v;
1447 do {
1448 r = rects[i];
1449 br = rects[i - 1];
1450 s5k5baf_bound_rect(r, br->width, br->height);
1451 } while (++i != R_INVALID);
1452 *v = *rects[first];
1455 static bool s5k5baf_cmp_rect(const struct v4l2_rect *r1,
1456 const struct v4l2_rect *r2)
1458 return !memcmp(r1, r2, sizeof(*r1));
1461 static int s5k5baf_set_selection(struct v4l2_subdev *sd,
1462 struct v4l2_subdev_pad_config *cfg,
1463 struct v4l2_subdev_selection *sel)
1465 static enum selection_rect rtype;
1466 struct s5k5baf *state = to_s5k5baf(sd);
1467 struct v4l2_rect **rects;
1468 int ret = 0;
1470 rtype = s5k5baf_get_sel_rect(sel->pad, sel->target);
1471 if (rtype == R_INVALID || s5k5baf_is_bound_target(sel->target))
1472 return -EINVAL;
1474 /* allow only scaling on compose */
1475 if (rtype == R_COMPOSE) {
1476 sel->r.left = 0;
1477 sel->r.top = 0;
1480 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1481 rects = (struct v4l2_rect * []) {
1482 &s5k5baf_cis_rect,
1483 v4l2_subdev_get_try_crop(sd, cfg, PAD_CIS),
1484 v4l2_subdev_get_try_compose(sd, cfg, PAD_CIS),
1485 v4l2_subdev_get_try_crop(sd, cfg, PAD_OUT)
1487 s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
1488 return 0;
1491 rects = (struct v4l2_rect * []) {
1492 &s5k5baf_cis_rect,
1493 &state->crop_sink,
1494 &state->compose,
1495 &state->crop_source
1497 mutex_lock(&state->lock);
1498 if (state->streaming) {
1499 /* adjust sel->r to avoid output resolution change */
1500 if (rtype < R_CROP_SOURCE) {
1501 if (sel->r.width < state->crop_source.width)
1502 sel->r.width = state->crop_source.width;
1503 if (sel->r.height < state->crop_source.height)
1504 sel->r.height = state->crop_source.height;
1505 } else {
1506 sel->r.width = state->crop_source.width;
1507 sel->r.height = state->crop_source.height;
1510 s5k5baf_set_rect_and_adjust(rects, rtype, &sel->r);
1511 if (!s5k5baf_cmp_rect(&state->crop_sink, &s5k5baf_cis_rect) ||
1512 !s5k5baf_cmp_rect(&state->compose, &s5k5baf_cis_rect))
1513 state->apply_crop = 1;
1514 if (state->streaming)
1515 ret = s5k5baf_hw_set_crop_rects(state);
1516 mutex_unlock(&state->lock);
1518 return ret;
1521 static const struct v4l2_subdev_pad_ops s5k5baf_cis_pad_ops = {
1522 .enum_mbus_code = s5k5baf_enum_mbus_code,
1523 .enum_frame_size = s5k5baf_enum_frame_size,
1524 .get_fmt = s5k5baf_get_fmt,
1525 .set_fmt = s5k5baf_set_fmt,
1528 static const struct v4l2_subdev_pad_ops s5k5baf_pad_ops = {
1529 .enum_mbus_code = s5k5baf_enum_mbus_code,
1530 .enum_frame_size = s5k5baf_enum_frame_size,
1531 .enum_frame_interval = s5k5baf_enum_frame_interval,
1532 .get_fmt = s5k5baf_get_fmt,
1533 .set_fmt = s5k5baf_set_fmt,
1534 .get_selection = s5k5baf_get_selection,
1535 .set_selection = s5k5baf_set_selection,
1538 static const struct v4l2_subdev_video_ops s5k5baf_video_ops = {
1539 .g_frame_interval = s5k5baf_g_frame_interval,
1540 .s_frame_interval = s5k5baf_s_frame_interval,
1541 .s_stream = s5k5baf_s_stream,
1545 * V4L2 subdev controls
1548 static int s5k5baf_s_ctrl(struct v4l2_ctrl *ctrl)
1550 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
1551 struct s5k5baf *state = to_s5k5baf(sd);
1552 int ret;
1554 v4l2_dbg(1, debug, sd, "ctrl: %s, value: %d\n", ctrl->name, ctrl->val);
1556 mutex_lock(&state->lock);
1558 if (state->power == 0)
1559 goto unlock;
1561 switch (ctrl->id) {
1562 case V4L2_CID_AUTO_WHITE_BALANCE:
1563 s5k5baf_hw_set_awb(state, ctrl->val);
1564 break;
1566 case V4L2_CID_BRIGHTNESS:
1567 s5k5baf_write(state, REG_USER_BRIGHTNESS, ctrl->val);
1568 break;
1570 case V4L2_CID_COLORFX:
1571 s5k5baf_hw_set_colorfx(state, ctrl->val);
1572 break;
1574 case V4L2_CID_CONTRAST:
1575 s5k5baf_write(state, REG_USER_CONTRAST, ctrl->val);
1576 break;
1578 case V4L2_CID_EXPOSURE_AUTO:
1579 s5k5baf_hw_set_auto_exposure(state, ctrl->val);
1580 break;
1582 case V4L2_CID_HFLIP:
1583 s5k5baf_hw_set_mirror(state);
1584 break;
1586 case V4L2_CID_POWER_LINE_FREQUENCY:
1587 s5k5baf_hw_set_anti_flicker(state, ctrl->val);
1588 break;
1590 case V4L2_CID_SATURATION:
1591 s5k5baf_write(state, REG_USER_SATURATION, ctrl->val);
1592 break;
1594 case V4L2_CID_SHARPNESS:
1595 s5k5baf_write(state, REG_USER_SHARPBLUR, ctrl->val);
1596 break;
1598 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
1599 s5k5baf_write(state, REG_P_COLORTEMP(0), ctrl->val);
1600 if (state->apply_cfg)
1601 s5k5baf_hw_sync_cfg(state);
1602 break;
1604 case V4L2_CID_TEST_PATTERN:
1605 s5k5baf_hw_set_test_pattern(state, ctrl->val);
1606 break;
1608 unlock:
1609 ret = s5k5baf_clear_error(state);
1610 mutex_unlock(&state->lock);
1611 return ret;
1614 static const struct v4l2_ctrl_ops s5k5baf_ctrl_ops = {
1615 .s_ctrl = s5k5baf_s_ctrl,
1618 static const char * const s5k5baf_test_pattern_menu[] = {
1619 "Disabled",
1620 "Blank",
1621 "Bars",
1622 "Gradients",
1623 "Textile",
1624 "Textile2",
1625 "Squares"
1628 static int s5k5baf_initialize_ctrls(struct s5k5baf *state)
1630 const struct v4l2_ctrl_ops *ops = &s5k5baf_ctrl_ops;
1631 struct s5k5baf_ctrls *ctrls = &state->ctrls;
1632 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
1633 int ret;
1635 ret = v4l2_ctrl_handler_init(hdl, 16);
1636 if (ret < 0) {
1637 v4l2_err(&state->sd, "cannot init ctrl handler (%d)\n", ret);
1638 return ret;
1641 /* Auto white balance cluster */
1642 ctrls->awb = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTO_WHITE_BALANCE,
1643 0, 1, 1, 1);
1644 ctrls->gain_red = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1645 0, 255, 1, S5K5BAF_GAIN_RED_DEF);
1646 ctrls->gain_blue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1647 0, 255, 1, S5K5BAF_GAIN_BLUE_DEF);
1648 v4l2_ctrl_auto_cluster(3, &ctrls->awb, 0, false);
1650 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1651 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1652 v4l2_ctrl_cluster(2, &ctrls->hflip);
1654 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
1655 V4L2_CID_EXPOSURE_AUTO,
1656 V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
1657 /* Exposure time: x 1 us */
1658 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
1659 0, 6000000U, 1, 100000U);
1660 /* Total gain: 256 <=> 1x */
1661 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1662 0, 256, 1, 256);
1663 v4l2_ctrl_auto_cluster(3, &ctrls->auto_exp, 0, false);
1665 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_POWER_LINE_FREQUENCY,
1666 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
1667 V4L2_CID_POWER_LINE_FREQUENCY_AUTO);
1669 v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_COLORFX,
1670 V4L2_COLORFX_SKY_BLUE, ~0x6f, V4L2_COLORFX_NONE);
1672 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
1673 0, 256, 1, 0);
1675 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
1676 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -127, 127, 1, 0);
1677 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
1678 v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -127, 127, 1, 0);
1680 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
1681 ARRAY_SIZE(s5k5baf_test_pattern_menu) - 1,
1682 0, 0, s5k5baf_test_pattern_menu);
1684 if (hdl->error) {
1685 v4l2_err(&state->sd, "error creating controls (%d)\n",
1686 hdl->error);
1687 ret = hdl->error;
1688 v4l2_ctrl_handler_free(hdl);
1689 return ret;
1692 state->sd.ctrl_handler = hdl;
1693 return 0;
1697 * V4L2 subdev internal operations
1699 static int s5k5baf_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1701 struct v4l2_mbus_framefmt *mf;
1703 mf = v4l2_subdev_get_try_format(sd, fh->pad, PAD_CIS);
1704 s5k5baf_try_cis_format(mf);
1706 if (s5k5baf_is_cis_subdev(sd))
1707 return 0;
1709 mf = v4l2_subdev_get_try_format(sd, fh->pad, PAD_OUT);
1710 mf->colorspace = s5k5baf_formats[0].colorspace;
1711 mf->code = s5k5baf_formats[0].code;
1712 mf->width = s5k5baf_cis_rect.width;
1713 mf->height = s5k5baf_cis_rect.height;
1714 mf->field = V4L2_FIELD_NONE;
1716 *v4l2_subdev_get_try_crop(sd, fh->pad, PAD_CIS) = s5k5baf_cis_rect;
1717 *v4l2_subdev_get_try_compose(sd, fh->pad, PAD_CIS) = s5k5baf_cis_rect;
1718 *v4l2_subdev_get_try_crop(sd, fh->pad, PAD_OUT) = s5k5baf_cis_rect;
1720 return 0;
1723 static int s5k5baf_check_fw_revision(struct s5k5baf *state)
1725 u16 api_ver = 0, fw_rev = 0, s_id = 0;
1726 int ret;
1728 api_ver = s5k5baf_read(state, REG_FW_APIVER);
1729 fw_rev = s5k5baf_read(state, REG_FW_REVISION) & 0xff;
1730 s_id = s5k5baf_read(state, REG_FW_SENSOR_ID);
1731 ret = s5k5baf_clear_error(state);
1732 if (ret < 0)
1733 return ret;
1735 v4l2_info(&state->sd, "FW API=%#x, revision=%#x sensor_id=%#x\n",
1736 api_ver, fw_rev, s_id);
1738 if (api_ver != S5K5BAF_FW_APIVER) {
1739 v4l2_err(&state->sd, "FW API version not supported\n");
1740 return -ENODEV;
1743 return 0;
1746 static int s5k5baf_registered(struct v4l2_subdev *sd)
1748 struct s5k5baf *state = to_s5k5baf(sd);
1749 int ret;
1751 ret = v4l2_device_register_subdev(sd->v4l2_dev, &state->cis_sd);
1752 if (ret < 0)
1753 v4l2_err(sd, "failed to register subdev %s\n",
1754 state->cis_sd.name);
1755 else
1756 ret = media_create_pad_link(&state->cis_sd.entity, PAD_CIS,
1757 &state->sd.entity, PAD_CIS,
1758 MEDIA_LNK_FL_IMMUTABLE |
1759 MEDIA_LNK_FL_ENABLED);
1760 return ret;
1763 static void s5k5baf_unregistered(struct v4l2_subdev *sd)
1765 struct s5k5baf *state = to_s5k5baf(sd);
1766 v4l2_device_unregister_subdev(&state->cis_sd);
1769 static const struct v4l2_subdev_ops s5k5baf_cis_subdev_ops = {
1770 .pad = &s5k5baf_cis_pad_ops,
1773 static const struct v4l2_subdev_internal_ops s5k5baf_cis_subdev_internal_ops = {
1774 .open = s5k5baf_open,
1777 static const struct v4l2_subdev_internal_ops s5k5baf_subdev_internal_ops = {
1778 .registered = s5k5baf_registered,
1779 .unregistered = s5k5baf_unregistered,
1780 .open = s5k5baf_open,
1783 static const struct v4l2_subdev_core_ops s5k5baf_core_ops = {
1784 .s_power = s5k5baf_set_power,
1785 .log_status = v4l2_ctrl_subdev_log_status,
1788 static const struct v4l2_subdev_ops s5k5baf_subdev_ops = {
1789 .core = &s5k5baf_core_ops,
1790 .pad = &s5k5baf_pad_ops,
1791 .video = &s5k5baf_video_ops,
1794 static int s5k5baf_configure_gpios(struct s5k5baf *state)
1796 static const char * const name[] = { "S5K5BAF_STBY", "S5K5BAF_RST" };
1797 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
1798 struct s5k5baf_gpio *g = state->gpios;
1799 int ret, i;
1801 for (i = 0; i < NUM_GPIOS; ++i) {
1802 int flags = GPIOF_DIR_OUT;
1803 if (g[i].level)
1804 flags |= GPIOF_INIT_HIGH;
1805 ret = devm_gpio_request_one(&c->dev, g[i].gpio, flags, name[i]);
1806 if (ret < 0) {
1807 v4l2_err(c, "failed to request gpio %s\n", name[i]);
1808 return ret;
1811 return 0;
1814 static int s5k5baf_parse_gpios(struct s5k5baf_gpio *gpios, struct device *dev)
1816 static const char * const names[] = {
1817 "stbyn-gpios",
1818 "rstn-gpios",
1820 struct device_node *node = dev->of_node;
1821 enum of_gpio_flags flags;
1822 int ret, i;
1824 for (i = 0; i < NUM_GPIOS; ++i) {
1825 ret = of_get_named_gpio_flags(node, names[i], 0, &flags);
1826 if (ret < 0) {
1827 dev_err(dev, "no %s GPIO pin provided\n", names[i]);
1828 return ret;
1830 gpios[i].gpio = ret;
1831 gpios[i].level = !(flags & OF_GPIO_ACTIVE_LOW);
1834 return 0;
1837 static int s5k5baf_parse_device_node(struct s5k5baf *state, struct device *dev)
1839 struct device_node *node = dev->of_node;
1840 struct device_node *node_ep;
1841 struct v4l2_fwnode_endpoint ep = { .bus_type = 0 };
1842 int ret;
1844 if (!node) {
1845 dev_err(dev, "no device-tree node provided\n");
1846 return -EINVAL;
1849 ret = of_property_read_u32(node, "clock-frequency",
1850 &state->mclk_frequency);
1851 if (ret < 0) {
1852 state->mclk_frequency = S5K5BAF_DEFAULT_MCLK_FREQ;
1853 dev_info(dev, "using default %u Hz clock frequency\n",
1854 state->mclk_frequency);
1857 ret = s5k5baf_parse_gpios(state->gpios, dev);
1858 if (ret < 0)
1859 return ret;
1861 node_ep = of_graph_get_next_endpoint(node, NULL);
1862 if (!node_ep) {
1863 dev_err(dev, "no endpoint defined at node %pOF\n", node);
1864 return -EINVAL;
1867 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(node_ep), &ep);
1868 of_node_put(node_ep);
1869 if (ret)
1870 return ret;
1872 state->bus_type = ep.bus_type;
1874 switch (state->bus_type) {
1875 case V4L2_MBUS_CSI2_DPHY:
1876 state->nlanes = ep.bus.mipi_csi2.num_data_lanes;
1877 break;
1878 case V4L2_MBUS_PARALLEL:
1879 break;
1880 default:
1881 dev_err(dev, "unsupported bus in endpoint defined at node %pOF\n",
1882 node);
1883 return -EINVAL;
1886 return 0;
1889 static int s5k5baf_configure_subdevs(struct s5k5baf *state,
1890 struct i2c_client *c)
1892 struct v4l2_subdev *sd;
1893 int ret;
1895 sd = &state->cis_sd;
1896 v4l2_subdev_init(sd, &s5k5baf_cis_subdev_ops);
1897 sd->owner = THIS_MODULE;
1898 v4l2_set_subdevdata(sd, state);
1899 snprintf(sd->name, sizeof(sd->name), "S5K5BAF-CIS %d-%04x",
1900 i2c_adapter_id(c->adapter), c->addr);
1902 sd->internal_ops = &s5k5baf_cis_subdev_internal_ops;
1903 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1905 state->cis_pad.flags = MEDIA_PAD_FL_SOURCE;
1906 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1907 ret = media_entity_pads_init(&sd->entity, NUM_CIS_PADS, &state->cis_pad);
1908 if (ret < 0)
1909 goto err;
1911 sd = &state->sd;
1912 v4l2_i2c_subdev_init(sd, c, &s5k5baf_subdev_ops);
1913 snprintf(sd->name, sizeof(sd->name), "S5K5BAF-ISP %d-%04x",
1914 i2c_adapter_id(c->adapter), c->addr);
1916 sd->internal_ops = &s5k5baf_subdev_internal_ops;
1917 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1919 state->pads[PAD_CIS].flags = MEDIA_PAD_FL_SINK;
1920 state->pads[PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
1921 sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
1922 ret = media_entity_pads_init(&sd->entity, NUM_ISP_PADS, state->pads);
1924 if (!ret)
1925 return 0;
1927 media_entity_cleanup(&state->cis_sd.entity);
1928 err:
1929 dev_err(&c->dev, "cannot init media entity %s\n", sd->name);
1930 return ret;
1933 static int s5k5baf_configure_regulators(struct s5k5baf *state)
1935 struct i2c_client *c = v4l2_get_subdevdata(&state->sd);
1936 int ret;
1937 int i;
1939 for (i = 0; i < S5K5BAF_NUM_SUPPLIES; i++)
1940 state->supplies[i].supply = s5k5baf_supply_names[i];
1942 ret = devm_regulator_bulk_get(&c->dev, S5K5BAF_NUM_SUPPLIES,
1943 state->supplies);
1944 if (ret < 0)
1945 v4l2_err(c, "failed to get regulators\n");
1946 return ret;
1949 static int s5k5baf_probe(struct i2c_client *c)
1951 struct s5k5baf *state;
1952 int ret;
1954 state = devm_kzalloc(&c->dev, sizeof(*state), GFP_KERNEL);
1955 if (!state)
1956 return -ENOMEM;
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;
1963 ret = s5k5baf_parse_device_node(state, &c->dev);
1964 if (ret < 0)
1965 return ret;
1967 ret = s5k5baf_configure_subdevs(state, c);
1968 if (ret < 0)
1969 return ret;
1971 ret = s5k5baf_configure_gpios(state);
1972 if (ret < 0)
1973 goto err_me;
1975 ret = s5k5baf_configure_regulators(state);
1976 if (ret < 0)
1977 goto err_me;
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;
1985 ret = s5k5baf_power_on(state);
1986 if (ret < 0) {
1987 ret = -EPROBE_DEFER;
1988 goto err_me;
1990 s5k5baf_hw_init(state);
1991 ret = s5k5baf_check_fw_revision(state);
1993 s5k5baf_power_off(state);
1994 if (ret < 0)
1995 goto err_me;
1997 ret = s5k5baf_initialize_ctrls(state);
1998 if (ret < 0)
1999 goto err_me;
2001 ret = v4l2_async_register_subdev(&state->sd);
2002 if (ret < 0)
2003 goto err_ctrl;
2005 return 0;
2007 err_ctrl:
2008 v4l2_ctrl_handler_free(state->sd.ctrl_handler);
2009 err_me:
2010 media_entity_cleanup(&state->sd.entity);
2011 media_entity_cleanup(&state->cis_sd.entity);
2012 return ret;
2015 static int s5k5baf_remove(struct i2c_client *c)
2017 struct v4l2_subdev *sd = i2c_get_clientdata(c);
2018 struct s5k5baf *state = to_s5k5baf(sd);
2020 v4l2_async_unregister_subdev(sd);
2021 v4l2_ctrl_handler_free(sd->ctrl_handler);
2022 media_entity_cleanup(&sd->entity);
2024 sd = &state->cis_sd;
2025 v4l2_device_unregister_subdev(sd);
2026 media_entity_cleanup(&sd->entity);
2028 return 0;
2031 static const struct i2c_device_id s5k5baf_id[] = {
2032 { S5K5BAF_DRIVER_NAME, 0 },
2033 { },
2035 MODULE_DEVICE_TABLE(i2c, s5k5baf_id);
2037 static const struct of_device_id s5k5baf_of_match[] = {
2038 { .compatible = "samsung,s5k5baf" },
2041 MODULE_DEVICE_TABLE(of, s5k5baf_of_match);
2043 static struct i2c_driver s5k5baf_i2c_driver = {
2044 .driver = {
2045 .of_match_table = s5k5baf_of_match,
2046 .name = S5K5BAF_DRIVER_NAME
2048 .probe_new = s5k5baf_probe,
2049 .remove = s5k5baf_remove,
2050 .id_table = s5k5baf_id,
2053 module_i2c_driver(s5k5baf_i2c_driver);
2055 MODULE_DESCRIPTION("Samsung S5K5BAF(X) UXGA camera driver");
2056 MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
2057 MODULE_LICENSE("GPL v2");