1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * imx323 driver
4 *
5 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
6 *
7 * V0.0X01.0X01 add poweron function.
8 * V0.0X01.0X02 add enum_frame_interval function.
9 * V0.0X01.0X03 add quick stream on/off
10 */
11
12 #include <linux/clk.h>
13 #include <linux/device.h>
14 #include <linux/delay.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/module.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/sysfs.h>
22 #include <linux/version.h>
23 #include <linux/rk-camera-module.h>
24 #include <media/media-entity.h>
25 #include <media/v4l2-async.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-subdev.h>
28
29 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x03)
30
31 #ifndef V4L2_CID_DIGITAL_GAIN
32 #define V4L2_CID_DIGITAL_GAIN V4L2_CID_GAIN
33 #endif
34
35 /* 74.25Mhz */
36 #define IMX323_PIXEL_RATE (74250 * 1000)
37 #define IMX323_XVCLK_FREQ 37125000
38
39 #define CHIP_ID 0xa
40 #define IMX323_REG_CHIP_ID 0x0112
41
42 #define IMX323_REG_CTRL_MODE 0x0100
43 #define IMX323_MODE_SW_STANDBY 0x0
44 #define IMX323_MODE_STREAMING BIT(0)
45
46 #define IMX323_REG_EXPOSURE 0x0202
47 #define IMX323_EXPOSURE_MIN 0
48 #define IMX323_EXPOSURE_STEP 1
49 #define IMX323_VTS_MAX 0x465
50
51 #define IMX323_REG_ANALOG_GAIN 0x301e
52 #define ANALOG_GAIN_MIN 0x0
53 #define ANALOG_GAIN_MAX 0x78
54 #define ANALOG_GAIN_STEP 1
55 #define ANALOG_GAIN_DEFAULT 0x10
56
57 #define IMX323_REG_VTS 0x0340
58
59 #define IMX323_REG_ORIENTATION 0x0101
60 #define IMX323_ORIENTATION_H 0x1
61 #define IMX323_ORIENTATION_V 0x2
62
63 #define REG_NULL 0xFFFF
64
65 #define IMX323_REG_VALUE_08BIT 1
66 #define IMX323_REG_VALUE_16BIT 2
67 #define IMX323_REG_VALUE_24BIT 3
68
69 /* h_offs 35 v_offs 14 */
70 #define PIX_FORMAT MEDIA_BUS_FMT_SBGGR10_1X10
71
72 #define IMX323_NAME "imx323"
73
74 struct cam_regulator {
75 char name[32];
76 int val;
77 };
78
79 static const struct cam_regulator imx323_regulator[] = {
80 {"avdd", 2800000}, /* Analog power */
81 {"dovdd", 1800000}, /* Digital I/O power */
82 {"dvdd", 1200000}, /* Digital core power */
83 };
84
85 #define IMX323_NUM_SUPPLIES ARRAY_SIZE(imx323_regulator)
86
87 struct regval {
88 u16 addr;
89 u8 val;
90 };
91
92 struct imx323_mode {
93 u32 width;
94 u32 height;
95 struct v4l2_fract max_fps;
96 u32 hts_def;
97 u32 vts_def;
98 u32 exp_def;
99 const struct regval *reg_list;
100 };
101
102 struct imx323 {
103 struct i2c_client *client;
104 struct clk *xvclk;
105 struct gpio_desc *reset_gpio;
106 struct gpio_desc *pwdn_gpio;
107 struct regulator_bulk_data supplies[IMX323_NUM_SUPPLIES];
108
109 struct v4l2_subdev subdev;
110 struct media_pad pad;
111 struct v4l2_ctrl_handler ctrl_handler;
112 struct v4l2_ctrl *exposure;
113 struct v4l2_ctrl *anal_gain;
114 struct v4l2_ctrl *digi_gain;
115 struct v4l2_ctrl *hblank;
116 struct v4l2_ctrl *vblank;
117 struct v4l2_ctrl *test_pattern;
118 struct mutex mutex;
119 bool streaming;
120 bool power_on;
121 const struct imx323_mode *cur_mode;
122 u32 module_index;
123 const char *module_facing;
124 const char *module_name;
125 const char *len_name;
126 };
127
128 #define to_imx323(sd) container_of(sd, struct imx323, subdev)
129
130 /*
131 * Xclk 37.125Mhz
132 * Pclk 74.25Mhz
133 * linelength 2200(0x44c * 2)
134 * framelength 1125(0x465)
135 * grabwindow_width 1920
136 * grabwindow_height 1080
137 * max_framerate 30fps
138 * dvp bt656 10bit
139 */
140 static const struct regval imx323_regs[] = {
141 {0x0100, 0x00},
142 {0x0009, 0x3f},
143 {0x0340, 0x04},
144 {0x0341, 0x65},
145 {0x0342, 0x04},
146 {0x0343, 0x4c},
147 {0x3000, 0x31},
148 {0x3002, 0x0f},
149 {0x3011, 0x00},
150 {0x3013, 0x40},
151 {0x3016, 0x3c},
152 {0x301a, 0x51},
153 {0x301f, 0x73},
154 {0x3021, 0x80},
155 {0x3022, 0x40},
156 {0x3027, 0x20},
157 {0x302c, 0x00},
158 {0x302d, 0x48}, /* low 10bit */
159 {0x304f, 0x47},
160 {0x3054, 0x10},
161 {0x307a, 0x40},
162 {0x307b, 0x02},
163 {0x3117, 0x0d},
164 {REG_NULL, 0x00},
165 };
166
167 static const struct imx323_mode supported_modes[] = {
168 {
169 .width = 2200,
170 .height = 1125,
171 .max_fps = {
172 .numerator = 10000,
173 .denominator = 300000,
174 },
175 .exp_def = 0x0100,
176 .hts_def = 0x044c * 2,
177 .vts_def = 0x0465,
178 .reg_list = imx323_regs,
179 }
180 };
181
182 static const char * const imx323_test_pattern_menu[] = {
183 "Disabled",
184 "Vertical Color Bar Type 1",
185 "Vertical Color Bar Type 2",
186 "Vertical Color Bar Type 3",
187 "Vertical Color Bar Type 4"
188 };
189
190 /* Write registers up to 4 at a time */
imx323_write_reg(struct i2c_client * client,u16 reg,int len,u32 val)191 static int imx323_write_reg(struct i2c_client *client, u16 reg,
192 int len, u32 val)
193 {
194 u32 buf_i, val_i;
195 u8 buf[6];
196 u8 *val_p;
197 __be32 val_be;
198
199 if (len > 4)
200 return -EINVAL;
201
202 buf[0] = reg >> 8;
203 buf[1] = reg & 0xff;
204
205 val_be = cpu_to_be32(val);
206 val_p = (u8 *)&val_be;
207 buf_i = 2;
208 val_i = 4 - len;
209
210 while (val_i < 4)
211 buf[buf_i++] = val_p[val_i++];
212
213 if (i2c_master_send(client, buf, len + 2) != len + 2)
214 return -EIO;
215
216 return 0;
217 }
218
imx323_write_array(struct i2c_client * client,const struct regval * regs)219 static int imx323_write_array(struct i2c_client *client,
220 const struct regval *regs)
221 {
222 u32 i;
223 int ret = 0;
224
225 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
226 ret = imx323_write_reg(client, regs[i].addr,
227 IMX323_REG_VALUE_08BIT, regs[i].val);
228
229 return ret;
230 }
231
232 /* Read registers up to 4 at a time */
imx323_read_reg(struct i2c_client * client,u16 reg,unsigned int len,u32 * val)233 static int imx323_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
234 u32 *val)
235 {
236 struct i2c_msg msgs[2];
237 u8 *data_be_p;
238 __be32 data_be = 0;
239 __be16 reg_addr_be = cpu_to_be16(reg);
240 int ret;
241
242 if (len > 4 || !len)
243 return -EINVAL;
244
245 data_be_p = (u8 *)&data_be;
246 /* Write register address */
247 msgs[0].addr = client->addr;
248 msgs[0].flags = 0;
249 msgs[0].len = 2;
250 msgs[0].buf = (u8 *)®_addr_be;
251
252 /* Read data from register */
253 msgs[1].addr = client->addr;
254 msgs[1].flags = I2C_M_RD;
255 msgs[1].len = len;
256 msgs[1].buf = &data_be_p[4 - len];
257
258 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
259 if (ret != ARRAY_SIZE(msgs))
260 return -EIO;
261
262 *val = be32_to_cpu(data_be);
263
264 return 0;
265 }
266
imx323_get_reso_dist(const struct imx323_mode * mode,struct v4l2_mbus_framefmt * framefmt)267 static int imx323_get_reso_dist(const struct imx323_mode *mode,
268 struct v4l2_mbus_framefmt *framefmt)
269 {
270 return abs(mode->width - framefmt->width) +
271 abs(mode->height - framefmt->height);
272 }
273
274 static const struct imx323_mode *
imx323_find_best_fit(struct v4l2_subdev_format * fmt)275 imx323_find_best_fit(struct v4l2_subdev_format *fmt)
276 {
277 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
278 int dist;
279 int cur_best_fit = 0;
280 int cur_best_fit_dist = -1;
281 u32 i;
282
283 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
284 dist = imx323_get_reso_dist(&supported_modes[i], framefmt);
285 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
286 cur_best_fit_dist = dist;
287 cur_best_fit = i;
288 }
289 }
290
291 return &supported_modes[cur_best_fit];
292 }
293
imx323_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)294 static int imx323_set_fmt(struct v4l2_subdev *sd,
295 struct v4l2_subdev_pad_config *cfg,
296 struct v4l2_subdev_format *fmt)
297 {
298 struct imx323 *imx323 = to_imx323(sd);
299 const struct imx323_mode *mode;
300 s64 h_blank, vblank_def;
301
302 mutex_lock(&imx323->mutex);
303
304 mode = imx323_find_best_fit(fmt);
305 fmt->format.code = PIX_FORMAT;
306 fmt->format.width = mode->width;
307 fmt->format.height = mode->height;
308 fmt->format.field = V4L2_FIELD_NONE;
309 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
310 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
311 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
312 #else
313 mutex_unlock(&imx323->mutex);
314 return -ENOTTY;
315 #endif
316 } else {
317 imx323->cur_mode = mode;
318 h_blank = mode->hts_def - mode->width;
319 __v4l2_ctrl_modify_range(imx323->hblank, h_blank,
320 h_blank, 1, h_blank);
321 vblank_def = mode->vts_def - mode->height;
322 __v4l2_ctrl_modify_range(imx323->vblank, vblank_def,
323 IMX323_VTS_MAX - mode->height,
324 1, vblank_def);
325 }
326
327 mutex_unlock(&imx323->mutex);
328
329 return 0;
330 }
331
imx323_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)332 static int imx323_get_fmt(struct v4l2_subdev *sd,
333 struct v4l2_subdev_pad_config *cfg,
334 struct v4l2_subdev_format *fmt)
335 {
336 struct imx323 *imx323 = to_imx323(sd);
337 const struct imx323_mode *mode = imx323->cur_mode;
338
339 mutex_lock(&imx323->mutex);
340 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
341 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
342 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
343 #else
344 mutex_unlock(&imx323->mutex);
345 return -ENOTTY;
346 #endif
347 } else {
348 fmt->format.width = mode->width;
349 fmt->format.height = mode->height;
350 fmt->format.code = PIX_FORMAT;
351 fmt->format.field = V4L2_FIELD_NONE;
352 }
353 mutex_unlock(&imx323->mutex);
354
355 return 0;
356 }
357
imx323_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)358 static int imx323_enum_mbus_code(struct v4l2_subdev *sd,
359 struct v4l2_subdev_pad_config *cfg,
360 struct v4l2_subdev_mbus_code_enum *code)
361 {
362 if (code->index != 0)
363 return -EINVAL;
364 code->code = PIX_FORMAT;
365
366 return 0;
367 }
368
imx323_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)369 static int imx323_enum_frame_sizes(struct v4l2_subdev *sd,
370 struct v4l2_subdev_pad_config *cfg,
371 struct v4l2_subdev_frame_size_enum *fse)
372 {
373 if (fse->index >= ARRAY_SIZE(supported_modes))
374 return -EINVAL;
375
376 if (fse->code != PIX_FORMAT)
377 return -EINVAL;
378
379 fse->min_width = supported_modes[fse->index].width;
380 fse->max_width = supported_modes[fse->index].width;
381 fse->max_height = supported_modes[fse->index].height;
382 fse->min_height = supported_modes[fse->index].height;
383
384 return 0;
385 }
386
imx323_enable_test_pattern(struct imx323 * imx323,u32 pattern)387 static int imx323_enable_test_pattern(struct imx323 *imx323, u32 pattern)
388 {
389 return 0;
390 }
391
imx323_get_module_inf(struct imx323 * imx323,struct rkmodule_inf * inf)392 static void imx323_get_module_inf(struct imx323 *imx323,
393 struct rkmodule_inf *inf)
394 {
395 memset(inf, 0, sizeof(*inf));
396 strlcpy(inf->base.sensor, IMX323_NAME, sizeof(inf->base.sensor));
397 strlcpy(inf->base.module, imx323->module_name,
398 sizeof(inf->base.module));
399 strlcpy(inf->base.lens, imx323->len_name, sizeof(inf->base.lens));
400 }
401
imx323_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)402 static long imx323_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
403 {
404 struct imx323 *imx323 = to_imx323(sd);
405 long ret = 0;
406 u32 stream = 0;
407
408 switch (cmd) {
409 case RKMODULE_GET_MODULE_INFO:
410 imx323_get_module_inf(imx323, (struct rkmodule_inf *)arg);
411 break;
412
413 case RKMODULE_SET_QUICK_STREAM:
414
415 stream = *((u32 *)arg);
416
417 if (stream)
418 imx323_write_reg(imx323->client, IMX323_REG_CTRL_MODE,
419 IMX323_REG_VALUE_08BIT, IMX323_MODE_STREAMING);
420 else
421 imx323_write_reg(imx323->client, IMX323_REG_CTRL_MODE,
422 IMX323_REG_VALUE_08BIT, IMX323_MODE_SW_STANDBY);
423 break;
424
425 case RKMODULE_GET_BT656_INTF_TYPE:
426 *(__u32 *)arg = BT656_SONY_RAW;
427 break;
428
429 default:
430 ret = -ENOIOCTLCMD;
431 break;
432 }
433
434 return ret;
435 }
436
437 #ifdef CONFIG_COMPAT
imx323_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)438 static long imx323_compat_ioctl32(struct v4l2_subdev *sd,
439 unsigned int cmd, unsigned long arg)
440 {
441 void __user *up = compat_ptr(arg);
442 struct rkmodule_inf *inf;
443 struct rkmodule_awb_cfg *cfg;
444 __u32 intf;
445 long ret;
446 u32 stream = 0;
447
448 switch (cmd) {
449 case RKMODULE_GET_MODULE_INFO:
450 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
451 if (!inf) {
452 ret = -ENOMEM;
453 return ret;
454 }
455
456 ret = imx323_ioctl(sd, cmd, inf);
457 if (!ret)
458 ret = copy_to_user(up, inf, sizeof(*inf));
459 kfree(inf);
460 break;
461 case RKMODULE_AWB_CFG:
462 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
463 if (!cfg) {
464 ret = -ENOMEM;
465 return ret;
466 }
467
468 ret = copy_from_user(cfg, up, sizeof(*cfg));
469 if (!ret)
470 ret = imx323_ioctl(sd, cmd, cfg);
471 kfree(cfg);
472 break;
473
474 case RKMODULE_SET_QUICK_STREAM:
475 ret = copy_from_user(&stream, up, sizeof(u32));
476 if (!ret)
477 ret = imx323_ioctl(sd, cmd, &stream);
478 break;
479
480 case RKMODULE_GET_BT656_INTF_TYPE:
481 intf = BT656_SONY_RAW;
482
483 ret = copy_to_user(up, &intf, sizeof(intf));
484 break;
485 default:
486 ret = -ENOIOCTLCMD;
487 break;
488 }
489
490 return ret;
491 }
492 #endif
493
__imx323_start_stream(struct imx323 * imx323)494 static int __imx323_start_stream(struct imx323 *imx323)
495 {
496 int ret;
497
498 ret = imx323_write_array(imx323->client, imx323->cur_mode->reg_list);
499 if (ret)
500 return ret;
501
502 /* In case these controls are set before streaming */
503 mutex_unlock(&imx323->mutex);
504 ret = v4l2_ctrl_handler_setup(&imx323->ctrl_handler);
505 mutex_lock(&imx323->mutex);
506 if (ret)
507 return ret;
508
509 return imx323_write_reg(imx323->client, IMX323_REG_CTRL_MODE,
510 IMX323_REG_VALUE_08BIT, IMX323_MODE_STREAMING);
511 }
512
__imx323_stop_stream(struct imx323 * imx323)513 static int __imx323_stop_stream(struct imx323 *imx323)
514 {
515 return imx323_write_reg(imx323->client, IMX323_REG_CTRL_MODE,
516 IMX323_REG_VALUE_08BIT, IMX323_MODE_SW_STANDBY);
517 }
518
imx323_s_stream(struct v4l2_subdev * sd,int on)519 static int imx323_s_stream(struct v4l2_subdev *sd, int on)
520 {
521 struct imx323 *imx323 = to_imx323(sd);
522 struct i2c_client *client = imx323->client;
523 int ret = 0;
524
525 mutex_lock(&imx323->mutex);
526 on = !!on;
527 if (on == imx323->streaming)
528 goto unlock_and_return;
529
530 if (on) {
531 ret = pm_runtime_get_sync(&client->dev);
532 if (ret < 0) {
533 pm_runtime_put_noidle(&client->dev);
534 goto unlock_and_return;
535 }
536
537 ret = __imx323_start_stream(imx323);
538 if (ret) {
539 v4l2_err(sd, "start stream failed while write regs\n");
540 pm_runtime_put(&client->dev);
541 goto unlock_and_return;
542 }
543 } else {
544 __imx323_stop_stream(imx323);
545 pm_runtime_put(&client->dev);
546 }
547
548 imx323->streaming = on;
549 unlock_and_return:
550 mutex_unlock(&imx323->mutex);
551
552 return ret;
553 }
554
imx323_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)555 static int imx323_g_frame_interval(struct v4l2_subdev *sd,
556 struct v4l2_subdev_frame_interval *fi)
557 {
558 struct imx323 *imx323 = to_imx323(sd);
559 const struct imx323_mode *mode = imx323->cur_mode;
560
561 fi->interval = mode->max_fps;
562
563 return 0;
564 }
565
imx323_s_power(struct v4l2_subdev * sd,int on)566 static int imx323_s_power(struct v4l2_subdev *sd, int on)
567 {
568 struct imx323 *imx323 = to_imx323(sd);
569 struct i2c_client *client = imx323->client;
570 int ret = 0;
571
572 mutex_lock(&imx323->mutex);
573
574 /* If the power state is not modified - no work to do. */
575 if (imx323->power_on == !!on)
576 goto unlock_and_return;
577
578 if (on) {
579 ret = pm_runtime_get_sync(&client->dev);
580 if (ret < 0) {
581 pm_runtime_put_noidle(&client->dev);
582 goto unlock_and_return;
583 }
584
585 imx323->power_on = true;
586 } else {
587 pm_runtime_put(&client->dev);
588 imx323->power_on = false;
589 }
590
591 unlock_and_return:
592 mutex_unlock(&imx323->mutex);
593
594 return ret;
595 }
596
597 /* Calculate the delay in us by clock rate and clock cycles */
imx323_cal_delay(u32 cycles)598 static inline u32 imx323_cal_delay(u32 cycles)
599 {
600 return DIV_ROUND_UP(cycles, IMX323_XVCLK_FREQ / 1000 / 1000);
601 }
602
__imx323_power_on(struct imx323 * imx323)603 static int __imx323_power_on(struct imx323 *imx323)
604 {
605 int ret;
606 u32 i, delay_us;
607 struct device *dev = &imx323->client->dev;
608
609 ret = clk_set_rate(imx323->xvclk, IMX323_XVCLK_FREQ);
610 if (ret < 0) {
611 dev_err(dev, "Failed to set xvclk rate (%d)\n",
612 IMX323_XVCLK_FREQ);
613 return ret;
614 }
615 if (clk_get_rate(imx323->xvclk) != IMX323_XVCLK_FREQ)
616 dev_warn(dev, "xvclk mismatched, modes are based on %d\n",
617 IMX323_XVCLK_FREQ);
618 ret = clk_prepare_enable(imx323->xvclk);
619 if (ret < 0) {
620 dev_err(dev, "Failed to enable xvclk\n");
621 return ret;
622 }
623
624 if (!IS_ERR(imx323->reset_gpio))
625 gpiod_set_value_cansleep(imx323->reset_gpio, 0);
626
627 for (i = 0; i < IMX323_NUM_SUPPLIES; i++)
628 regulator_set_voltage(imx323->supplies[i].consumer,
629 imx323_regulator[i].val,
630 imx323_regulator[i].val);
631
632 ret = regulator_bulk_enable(IMX323_NUM_SUPPLIES, imx323->supplies);
633 if (ret < 0) {
634 dev_err(dev, "Failed to enable regulators\n");
635 goto disable_clk;
636 }
637
638 if (!IS_ERR(imx323->reset_gpio))
639 gpiod_set_value_cansleep(imx323->reset_gpio, 1);
640
641 if (!IS_ERR(imx323->pwdn_gpio))
642 gpiod_set_value_cansleep(imx323->pwdn_gpio, 1);
643
644 /* 8192 cycles prior to first SCCB transaction */
645 delay_us = imx323_cal_delay(8192);
646 usleep_range(delay_us, delay_us * 2);
647
648 return 0;
649
650 disable_clk:
651 clk_disable_unprepare(imx323->xvclk);
652
653 return ret;
654 }
655
__imx323_power_off(struct imx323 * imx323)656 static void __imx323_power_off(struct imx323 *imx323)
657 {
658 if (!IS_ERR(imx323->pwdn_gpio))
659 gpiod_set_value_cansleep(imx323->pwdn_gpio, 0);
660 clk_disable_unprepare(imx323->xvclk);
661 if (!IS_ERR(imx323->reset_gpio))
662 gpiod_set_value_cansleep(imx323->reset_gpio, 0);
663 regulator_bulk_disable(IMX323_NUM_SUPPLIES, imx323->supplies);
664 }
665
imx323_runtime_resume(struct device * dev)666 static int imx323_runtime_resume(struct device *dev)
667 {
668 struct i2c_client *client = to_i2c_client(dev);
669 struct v4l2_subdev *sd = i2c_get_clientdata(client);
670 struct imx323 *imx323 = to_imx323(sd);
671
672 return __imx323_power_on(imx323);
673 }
674
imx323_runtime_suspend(struct device * dev)675 static int imx323_runtime_suspend(struct device *dev)
676 {
677 struct i2c_client *client = to_i2c_client(dev);
678 struct v4l2_subdev *sd = i2c_get_clientdata(client);
679 struct imx323 *imx323 = to_imx323(sd);
680
681 __imx323_power_off(imx323);
682
683 return 0;
684 }
685
686 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
imx323_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)687 static int imx323_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
688 {
689 struct imx323 *imx323 = to_imx323(sd);
690 struct v4l2_mbus_framefmt *try_fmt =
691 v4l2_subdev_get_try_format(sd, fh->pad, 0);
692 const struct imx323_mode *def_mode = &supported_modes[0];
693
694 mutex_lock(&imx323->mutex);
695 /* Initialize try_fmt */
696 try_fmt->width = def_mode->width;
697 try_fmt->height = def_mode->height;
698 try_fmt->code = PIX_FORMAT;
699 try_fmt->field = V4L2_FIELD_NONE;
700 mutex_unlock(&imx323->mutex);
701 /* No crop or compose */
702
703 return 0;
704 }
705 #endif
706
imx323_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * config)707 static int imx323_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
708 struct v4l2_mbus_config *config)
709 {
710 config->type = V4L2_MBUS_BT656;
711 config->flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
712 V4L2_MBUS_VSYNC_ACTIVE_HIGH |
713 V4L2_MBUS_PCLK_SAMPLE_FALLING;
714 return 0;
715 }
716
imx323_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)717 static int imx323_enum_frame_interval(struct v4l2_subdev *sd,
718 struct v4l2_subdev_pad_config *cfg,
719 struct v4l2_subdev_frame_interval_enum *fie)
720 {
721 if (fie->index >= ARRAY_SIZE(supported_modes))
722 return -EINVAL;
723
724 fie->code = PIX_FORMAT;
725 fie->width = supported_modes[fie->index].width;
726 fie->height = supported_modes[fie->index].height;
727 fie->interval = supported_modes[fie->index].max_fps;
728 return 0;
729 }
730
731 static const struct dev_pm_ops imx323_pm_ops = {
732 SET_RUNTIME_PM_OPS(imx323_runtime_suspend,
733 imx323_runtime_resume, NULL)
734 };
735
736 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
737 static const struct v4l2_subdev_internal_ops imx323_internal_ops = {
738 .open = imx323_open,
739 };
740 #endif
741
742 static const struct v4l2_subdev_core_ops imx323_core_ops = {
743 .s_power = imx323_s_power,
744 .ioctl = imx323_ioctl,
745 #ifdef CONFIG_COMPAT
746 .compat_ioctl32 = imx323_compat_ioctl32,
747 #endif
748 };
749
750 static const struct v4l2_subdev_video_ops imx323_video_ops = {
751 .s_stream = imx323_s_stream,
752 .g_frame_interval = imx323_g_frame_interval,
753 };
754
755 static const struct v4l2_subdev_pad_ops imx323_pad_ops = {
756 .enum_mbus_code = imx323_enum_mbus_code,
757 .enum_frame_size = imx323_enum_frame_sizes,
758 .enum_frame_interval = imx323_enum_frame_interval,
759 .get_fmt = imx323_get_fmt,
760 .set_fmt = imx323_set_fmt,
761 .get_mbus_config = imx323_g_mbus_config,
762 };
763
764 static const struct v4l2_subdev_ops imx323_subdev_ops = {
765 .core = &imx323_core_ops,
766 .video = &imx323_video_ops,
767 .pad = &imx323_pad_ops,
768 };
769
imx323_set_ctrl(struct v4l2_ctrl * ctrl)770 static int imx323_set_ctrl(struct v4l2_ctrl *ctrl)
771 {
772 struct imx323 *imx323 = container_of(ctrl->handler,
773 struct imx323, ctrl_handler);
774 struct i2c_client *client = imx323->client;
775 int ret = 0;
776
777 if (!pm_runtime_get_if_in_use(&client->dev))
778 return 0;
779
780 switch (ctrl->id) {
781 case V4L2_CID_EXPOSURE:
782 ret = imx323_write_reg(imx323->client, IMX323_REG_EXPOSURE,
783 IMX323_REG_VALUE_16BIT, ctrl->val);
784 break;
785 case V4L2_CID_ANALOGUE_GAIN:
786 ret = imx323_write_reg(imx323->client, IMX323_REG_ANALOG_GAIN,
787 IMX323_REG_VALUE_08BIT, ctrl->val);
788 break;
789 case V4L2_CID_VBLANK:
790 ret = imx323_write_reg(imx323->client, IMX323_REG_VTS,
791 IMX323_REG_VALUE_16BIT,
792 ctrl->val + imx323->cur_mode->height);
793 break;
794 case V4L2_CID_TEST_PATTERN:
795 ret = imx323_enable_test_pattern(imx323, ctrl->val);
796 break;
797 default:
798 dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
799 __func__, ctrl->id, ctrl->val);
800 break;
801 }
802
803 pm_runtime_put(&client->dev);
804
805 return ret;
806 }
807
808 static const struct v4l2_ctrl_ops imx323_ctrl_ops = {
809 .s_ctrl = imx323_set_ctrl,
810 };
811
imx323_initialize_controls(struct imx323 * imx323)812 static int imx323_initialize_controls(struct imx323 *imx323)
813 {
814 const struct imx323_mode *mode;
815 struct v4l2_ctrl_handler *handler;
816 s64 exposure_max, vblank_def;
817 u32 h_blank;
818 int ret;
819
820 handler = &imx323->ctrl_handler;
821 mode = imx323->cur_mode;
822 ret = v4l2_ctrl_handler_init(handler, 8);
823 if (ret)
824 return ret;
825 handler->lock = &imx323->mutex;
826
827 v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE,
828 0, IMX323_PIXEL_RATE, 1, IMX323_PIXEL_RATE);
829
830 h_blank = mode->hts_def - mode->width;
831 imx323->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
832 h_blank, h_blank, 1, h_blank);
833 if (imx323->hblank)
834 imx323->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
835
836 vblank_def = mode->vts_def - mode->height;
837 imx323->vblank = v4l2_ctrl_new_std(handler, &imx323_ctrl_ops,
838 V4L2_CID_VBLANK, vblank_def,
839 IMX323_VTS_MAX - mode->height,
840 1, vblank_def);
841
842 exposure_max = mode->vts_def - 1;
843 imx323->exposure = v4l2_ctrl_new_std(handler, &imx323_ctrl_ops,
844 V4L2_CID_EXPOSURE, IMX323_EXPOSURE_MIN,
845 exposure_max, IMX323_EXPOSURE_STEP,
846 mode->exp_def);
847
848 imx323->anal_gain = v4l2_ctrl_new_std(handler, &imx323_ctrl_ops,
849 V4L2_CID_ANALOGUE_GAIN, ANALOG_GAIN_MIN,
850 ANALOG_GAIN_MAX, ANALOG_GAIN_STEP,
851 ANALOG_GAIN_DEFAULT);
852
853 imx323->test_pattern = v4l2_ctrl_new_std_menu_items(handler,
854 &imx323_ctrl_ops, V4L2_CID_TEST_PATTERN,
855 ARRAY_SIZE(imx323_test_pattern_menu) - 1,
856 0, 0, imx323_test_pattern_menu);
857
858 if (handler->error) {
859 ret = handler->error;
860 dev_err(&imx323->client->dev,
861 "Failed to init controls(%d)\n", ret);
862 goto err_free_handler;
863 }
864
865 imx323->subdev.ctrl_handler = handler;
866
867 return 0;
868
869 err_free_handler:
870 v4l2_ctrl_handler_free(handler);
871
872 return ret;
873 }
874
imx323_check_sensor_id(struct imx323 * imx323,struct i2c_client * client)875 static int imx323_check_sensor_id(struct imx323 *imx323,
876 struct i2c_client *client)
877 {
878 struct device *dev = &imx323->client->dev;
879 u32 id = 0;
880 int ret;
881
882 ret = imx323_read_reg(client, IMX323_REG_CHIP_ID,
883 IMX323_REG_VALUE_08BIT, &id);
884 if (id != CHIP_ID) {
885 dev_err(dev, "Unexpected sensor id(%x), ret(%d)\n", id, ret);
886 return -ENODEV;
887 }
888
889 dev_info(dev, "Detected IMX323 sensor\n");
890
891 return 0;
892 }
893
imx323_configure_regulators(struct imx323 * imx323)894 static int imx323_configure_regulators(struct imx323 *imx323)
895 {
896 u32 i;
897
898 for (i = 0; i < IMX323_NUM_SUPPLIES; i++)
899 imx323->supplies[i].supply =
900 imx323_regulator[i].name;
901
902 return devm_regulator_bulk_get(&imx323->client->dev,
903 IMX323_NUM_SUPPLIES,
904 imx323->supplies);
905 }
906
imx323_probe(struct i2c_client * client,const struct i2c_device_id * id)907 static int imx323_probe(struct i2c_client *client,
908 const struct i2c_device_id *id)
909 {
910 struct device *dev = &client->dev;
911 struct device_node *node = dev->of_node;
912 struct imx323 *imx323;
913 struct v4l2_subdev *sd;
914 char facing[2];
915 int ret;
916
917 dev_info(dev, "driver version: %02x.%02x.%02x",
918 DRIVER_VERSION >> 16,
919 (DRIVER_VERSION & 0xff00) >> 8,
920 DRIVER_VERSION & 0x00ff);
921
922 imx323 = devm_kzalloc(dev, sizeof(*imx323), GFP_KERNEL);
923 if (!imx323)
924 return -ENOMEM;
925
926 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
927 &imx323->module_index);
928 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
929 &imx323->module_facing);
930 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
931 &imx323->module_name);
932 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
933 &imx323->len_name);
934 if (ret) {
935 dev_err(dev, "could not get module information!\n");
936 return -EINVAL;
937 }
938
939 imx323->client = client;
940 imx323->cur_mode = &supported_modes[0];
941
942 imx323->xvclk = devm_clk_get(dev, "xvclk");
943 if (IS_ERR(imx323->xvclk)) {
944 dev_err(dev, "Failed to get xvclk\n");
945 return -EINVAL;
946 }
947
948 imx323->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
949 if (IS_ERR(imx323->reset_gpio))
950 dev_warn(dev, "Failed to get reset-gpios\n");
951
952 imx323->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
953 if (IS_ERR(imx323->pwdn_gpio))
954 dev_warn(dev, "Failed to get pwdn-gpios\n");
955
956 ret = imx323_configure_regulators(imx323);
957 if (ret) {
958 dev_err(dev, "Failed to get power regulators\n");
959 return ret;
960 }
961
962 mutex_init(&imx323->mutex);
963
964 sd = &imx323->subdev;
965 v4l2_i2c_subdev_init(sd, client, &imx323_subdev_ops);
966 ret = imx323_initialize_controls(imx323);
967 if (ret)
968 goto err_destroy_mutex;
969
970 ret = __imx323_power_on(imx323);
971 if (ret)
972 goto err_free_handler;
973
974 ret = imx323_check_sensor_id(imx323, client);
975 if (ret)
976 goto err_power_off;
977
978 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
979 sd->internal_ops = &imx323_internal_ops;
980 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
981 V4L2_SUBDEV_FL_HAS_EVENTS;
982 #endif
983 #if defined(CONFIG_MEDIA_CONTROLLER)
984 imx323->pad.flags = MEDIA_PAD_FL_SOURCE;
985 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
986 ret = media_entity_pads_init(&sd->entity, 1, &imx323->pad);
987 if (ret < 0)
988 goto err_power_off;
989 #endif
990
991 memset(facing, 0, sizeof(facing));
992 if (strcmp(imx323->module_facing, "back") == 0)
993 facing[0] = 'b';
994 else
995 facing[0] = 'f';
996
997 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
998 imx323->module_index, facing,
999 IMX323_NAME, dev_name(sd->dev));
1000 ret = v4l2_async_register_subdev_sensor_common(sd);
1001 if (ret) {
1002 dev_err(dev, "v4l2 async register subdev failed\n");
1003 goto err_clean_entity;
1004 }
1005
1006 pm_runtime_set_active(dev);
1007 pm_runtime_enable(dev);
1008 pm_runtime_idle(dev);
1009
1010 return 0;
1011
1012 err_clean_entity:
1013 #if defined(CONFIG_MEDIA_CONTROLLER)
1014 media_entity_cleanup(&sd->entity);
1015 #endif
1016 err_power_off:
1017 __imx323_power_off(imx323);
1018 err_free_handler:
1019 v4l2_ctrl_handler_free(&imx323->ctrl_handler);
1020 err_destroy_mutex:
1021 mutex_destroy(&imx323->mutex);
1022
1023 return ret;
1024 }
1025
imx323_remove(struct i2c_client * client)1026 static int imx323_remove(struct i2c_client *client)
1027 {
1028 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1029 struct imx323 *imx323 = to_imx323(sd);
1030
1031 v4l2_async_unregister_subdev(sd);
1032 #if defined(CONFIG_MEDIA_CONTROLLER)
1033 media_entity_cleanup(&sd->entity);
1034 #endif
1035 v4l2_ctrl_handler_free(&imx323->ctrl_handler);
1036 mutex_destroy(&imx323->mutex);
1037
1038 pm_runtime_disable(&client->dev);
1039 if (!pm_runtime_status_suspended(&client->dev))
1040 __imx323_power_off(imx323);
1041 pm_runtime_set_suspended(&client->dev);
1042
1043 return 0;
1044 }
1045
1046 #if IS_ENABLED(CONFIG_OF)
1047 static const struct of_device_id imx323_of_match[] = {
1048 { .compatible = "sony,imx323" },
1049 {},
1050 };
1051 MODULE_DEVICE_TABLE(of, imx323_of_match);
1052 #endif
1053
1054 static const struct i2c_device_id imx323_match_id[] = {
1055 { "sony,imx323", 0 },
1056 { },
1057 };
1058
1059 static struct i2c_driver imx323_i2c_driver = {
1060 .driver = {
1061 .name = IMX323_NAME,
1062 .pm = &imx323_pm_ops,
1063 .of_match_table = of_match_ptr(imx323_of_match),
1064 },
1065 .probe = &imx323_probe,
1066 .remove = &imx323_remove,
1067 .id_table = imx323_match_id,
1068 };
1069
sensor_mod_init(void)1070 static int __init sensor_mod_init(void)
1071 {
1072 return i2c_add_driver(&imx323_i2c_driver);
1073 }
1074
sensor_mod_exit(void)1075 static void __exit sensor_mod_exit(void)
1076 {
1077 i2c_del_driver(&imx323_i2c_driver);
1078 }
1079
1080 device_initcall_sync(sensor_mod_init);
1081 module_exit(sensor_mod_exit);
1082
1083 MODULE_DESCRIPTION("Sony imx323 sensor driver");
1084 MODULE_LICENSE("GPL v2");
1085