1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * Rockchip rk1608 driver
4 *
5 * Copyright (C) 2017-2018 Rockchip Electronics Co., Ltd.
6 *
7 */
8 #include <linux/clk.h>
9 #include <linux/clkdev.h>
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/regmap.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/of_graph.h>
17 #include <linux/mfd/syscon.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-subdev.h>
21 #include <linux/platform_device.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/of_platform.h>
25 #include <linux/types.h>
26 #include <linux/rk-preisp.h>
27 #include <linux/rk-isp1-config.h>
28 #include <linux/rk-camera-module.h>
29 #include "rk1608_dphy.h"
30 #include <linux/compat.h>
31
32 #define RK1608_DPHY_NAME "RK1608-dphy"
33
34 /**
35 * Rk1608 is used as the Pre-ISP to link on Soc, which mainly has two
36 * functions. One is to download the firmware of RK1608, and the other
37 * is to match the extra sensor such as camera and enable sensor by
38 * calling sensor's s_power.
39 * |-----------------------|
40 * | Sensor Camera |
41 * |-----------------------|
42 * |-----------||----------|
43 * |-----------||----------|
44 * |-----------\/----------|
45 * | Pre-ISP RK1608 |
46 * |-----------------------|
47 * |-----------||----------|
48 * |-----------||----------|
49 * |-----------\/----------|
50 * | Rockchip Soc |
51 * |-----------------------|
52 * Data Transfer As shown above. In RK1608, the data received from the
53 * extra sensor,and it is passed to the Soc through ISP.
54 */
55
56 static DEFINE_MUTEX(rk1608_dphy_mutex);
to_state(struct v4l2_subdev * sd)57 static inline struct rk1608_dphy *to_state(struct v4l2_subdev *sd)
58 {
59 return container_of(sd, struct rk1608_dphy, sd);
60 }
61
rk1608_s_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)62 static int rk1608_s_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
63 {
64 struct v4l2_ctrl *remote_ctrl;
65 struct rk1608_dphy *pdata = to_state(sd);
66
67 pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
68 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
69 V4L2_CID_HBLANK);
70 if (remote_ctrl) {
71 v4l2_ctrl_g_ctrl(remote_ctrl);
72 __v4l2_ctrl_modify_range(pdata->hblank,
73 remote_ctrl->minimum,
74 remote_ctrl->maximum,
75 remote_ctrl->step,
76 remote_ctrl->default_value);
77 }
78
79 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
80 V4L2_CID_VBLANK);
81 if (remote_ctrl) {
82 v4l2_ctrl_g_ctrl(remote_ctrl);
83 __v4l2_ctrl_modify_range(pdata->vblank,
84 remote_ctrl->minimum,
85 remote_ctrl->maximum,
86 remote_ctrl->step,
87 remote_ctrl->default_value);
88 }
89
90 return 0;
91 }
92
rk1608_s_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)93 static int rk1608_s_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
94 {
95 struct rk1608_dphy *pdata = to_state(sd);
96
97 pdata->rk1608_sd->grp_id = sd->grp_id;
98
99 return 0;
100 }
101
rk1608_sensor_power(struct v4l2_subdev * sd,int on)102 static int rk1608_sensor_power(struct v4l2_subdev *sd, int on)
103 {
104 int ret = 0;
105 struct rk1608_dphy *pdata = to_state(sd);
106
107 pdata->rk1608_sd->grp_id = sd->grp_id;
108 ret = v4l2_subdev_call(pdata->rk1608_sd, core, s_power, on);
109
110 return ret;
111 }
112
113 #define RK1608_MAX_BITRATE (1500000000)
rk1608_get_link_sensor_timing(struct rk1608_dphy * pdata)114 static int rk1608_get_link_sensor_timing(struct rk1608_dphy *pdata)
115 {
116 int ret = 0;
117 u32 i;
118 u32 idx = pdata->fmt_inf_idx;
119 struct rk1608_fmt_inf *fmt_inf = &pdata->fmt_inf[idx];
120 int sub_sensor_num = pdata->sub_sensor_num;
121 u32 width = 0, height = 0, out_width, out_height;
122 struct v4l2_subdev *link_sensor;
123 u32 id = pdata->sd.grp_id;
124 struct v4l2_subdev_frame_interval fi;
125 int max_fps = 30;
126 u64 bps;
127
128 struct v4l2_subdev_format fmt = {
129 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
130 .pad = 0,
131 };
132
133 if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
134
135 link_sensor = i2c_get_clientdata(
136 pdata->link_sensor_client);
137 if (IS_ERR_OR_NULL(link_sensor)) {
138 dev_err(pdata->dev, "can not get link sensor i2c client\n");
139 return -EINVAL;
140 }
141
142 ret = v4l2_subdev_call(link_sensor, pad, get_fmt, NULL, &fmt);
143 if (ret) {
144 dev_info(pdata->dev, "get link fmt fail\n");
145 return -EINVAL;
146 }
147
148 width = fmt.format.width;
149 height = fmt.format.height;
150 dev_info(pdata->dev, "phy[%d] get fmt w:%d h:%d\n",
151 id, width, height);
152
153 memset(&fi, 0, sizeof(fi));
154 ret = v4l2_subdev_call(link_sensor, video, g_frame_interval, &fi);
155 if (ret) {
156 dev_info(pdata->dev, "get link interval fail\n");
157 return -EINVAL;
158 }
159
160 max_fps = fi.interval.denominator / fi.interval.numerator;
161 dev_info(pdata->dev, "phy[%d] get fps:%d (%d/%d)\n",
162 id, max_fps, fi.interval.denominator, fi.interval.numerator);
163
164 } else {
165 width = fmt_inf->mf.width;
166 height = fmt_inf->mf.height;
167 dev_info(pdata->dev, "phy[%d] no link sensor\n", id);
168 }
169
170 if (!width || !height) {
171 dev_err(pdata->dev, "phy[%d] get fmt error!\n", id);
172 return -EINVAL;
173 }
174
175 for (i = 0; i < 4; i++) {
176 if (fmt_inf->in_ch[i].width == 0)
177 break;
178
179 fmt_inf->in_ch[i].width = width;
180 fmt_inf->in_ch[i].height = height;
181 }
182
183 out_width = width;
184 out_height = height * (sub_sensor_num + 1); /* sub add main */
185 for (i = 0; i < 4; i++) {
186 if (fmt_inf->out_ch[i].width == 0)
187 break;
188
189 fmt_inf->out_ch[i].width = out_width;
190 fmt_inf->out_ch[i].height = out_height;
191 }
192
193 fmt_inf->hactive = out_width;
194 fmt_inf->vactive = out_height;
195 fmt_inf->htotal = out_width + (width * 1 / 3); //1.33
196 fmt_inf->vtotal = out_height + (height >> 4);
197
198 /* max 30 fps, raw 10 */
199 bps = fmt_inf->htotal * fmt_inf->vtotal
200 / fmt_inf->mipi_lane_out * 10 * max_fps;
201
202 /* add extra timing */
203 bps = bps * 105;
204 do_div(bps, 100);
205
206 if (bps > RK1608_MAX_BITRATE)
207 bps = RK1608_MAX_BITRATE;
208
209 pdata->link_freqs = (u32)(bps/2);
210 dev_info(pdata->dev, "target mipi bps:%lld\n", bps);
211
212 return 0;
213 }
214
rk1608_s_stream(struct v4l2_subdev * sd,int enable)215 static int rk1608_s_stream(struct v4l2_subdev *sd, int enable)
216 {
217 struct rk1608_dphy *pdata = to_state(sd);
218
219 if (enable && pdata->sub_sensor_num)
220 rk1608_get_link_sensor_timing(pdata);
221
222 pdata->rk1608_sd->grp_id = sd->grp_id;
223 v4l2_subdev_call(pdata->rk1608_sd, video, s_stream, enable);
224 return 0;
225 }
226
rk1608_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)227 static int rk1608_enum_mbus_code(struct v4l2_subdev *sd,
228 struct v4l2_subdev_pad_config *cfg,
229 struct v4l2_subdev_mbus_code_enum *code)
230 {
231 struct rk1608_dphy *pdata = to_state(sd);
232
233 if (code->index >= pdata->fmt_inf_num)
234 return -EINVAL;
235
236 code->code = pdata->fmt_inf[code->index].mf.code;
237
238 return 0;
239 }
240
rk1608_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)241 static int rk1608_enum_frame_sizes(struct v4l2_subdev *sd,
242 struct v4l2_subdev_pad_config *cfg,
243 struct v4l2_subdev_frame_size_enum *fse)
244 {
245 struct rk1608_dphy *pdata = to_state(sd);
246
247 if (fse->index >= pdata->fmt_inf_num)
248 return -EINVAL;
249
250 if (fse->code != pdata->fmt_inf[fse->index].mf.code)
251 return -EINVAL;
252
253 fse->min_width = pdata->fmt_inf[fse->index].mf.width;
254 fse->max_width = pdata->fmt_inf[fse->index].mf.width;
255 fse->max_height = pdata->fmt_inf[fse->index].mf.height;
256 fse->min_height = pdata->fmt_inf[fse->index].mf.height;
257
258 return 0;
259 }
260
rk1608_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)261 static int rk1608_get_fmt(struct v4l2_subdev *sd,
262 struct v4l2_subdev_pad_config *cfg,
263 struct v4l2_subdev_format *fmt)
264 {
265 struct v4l2_mbus_framefmt *mf = &fmt->format;
266 struct rk1608_dphy *pdata = to_state(sd);
267 u32 idx = pdata->fmt_inf_idx;
268 struct v4l2_subdev *link_sensor;
269 int ret = -1;
270
271 if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
272 link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
273 if (IS_ERR_OR_NULL(link_sensor)) {
274 dev_err(pdata->dev, "can not get link sensor i2c client\n");
275 goto exit;
276 }
277
278 ret = v4l2_subdev_call(link_sensor, pad, get_fmt, NULL, fmt);
279 if (ret) {
280 dev_info(pdata->dev, "get link fmt fail\n");
281 goto exit;
282 }
283
284 dev_info(pdata->dev, "use link sensor fmt w:%d h:%d code:%d\n",
285 mf->width, mf->height, mf->code);
286 }
287
288 exit:
289 if (ret || !mf->width || !mf->height) {
290 mf->code = pdata->fmt_inf[idx].mf.code;
291 mf->width = pdata->fmt_inf[idx].mf.width;
292 mf->height = pdata->fmt_inf[idx].mf.height;
293 mf->field = pdata->fmt_inf[idx].mf.field;
294 mf->colorspace = pdata->fmt_inf[idx].mf.colorspace;
295 } else {
296 pdata->fmt_inf[idx].mf.code = mf->code;
297 pdata->fmt_inf[idx].mf.width = mf->width;
298 pdata->fmt_inf[idx].mf.height = mf->height;
299 pdata->fmt_inf[idx].mf.field = mf->field;
300 pdata->fmt_inf[idx].mf.colorspace = mf->colorspace;
301 }
302
303 if (pdata->sub_sensor_num)
304 rk1608_get_link_sensor_timing(pdata);
305
306 return 0;
307 }
308
rk1608_get_reso_dist(struct rk1608_fmt_inf * fmt_inf,struct v4l2_subdev_format * fmt)309 static int rk1608_get_reso_dist(struct rk1608_fmt_inf *fmt_inf,
310 struct v4l2_subdev_format *fmt)
311 {
312 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
313
314 return abs(fmt_inf->mf.width - framefmt->width) +
315 abs(fmt_inf->mf.height - framefmt->height);
316 }
317
rk1608_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)318 static int rk1608_set_fmt(struct v4l2_subdev *sd,
319 struct v4l2_subdev_pad_config *cfg,
320 struct v4l2_subdev_format *fmt)
321 {
322 struct v4l2_ctrl *remote_ctrl;
323 struct rk1608_dphy *pdata = to_state(sd);
324 u32 i, idx = 0;
325 int dist;
326 int cur_best_fit_dist = -1;
327
328 for (i = 0; i < pdata->fmt_inf_num; i++) {
329 dist = rk1608_get_reso_dist(&pdata->fmt_inf[i], fmt);
330 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
331 cur_best_fit_dist = dist;
332 idx = i;
333 }
334 }
335
336 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
337 return -ENOTTY;
338
339 pdata->fmt_inf_idx = idx;
340
341 pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
342 v4l2_subdev_call(pdata->rk1608_sd, pad, set_fmt, cfg, fmt);
343
344 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
345 V4L2_CID_HBLANK);
346 if (remote_ctrl) {
347 v4l2_ctrl_g_ctrl(remote_ctrl);
348 __v4l2_ctrl_modify_range(pdata->hblank,
349 remote_ctrl->minimum,
350 remote_ctrl->maximum,
351 remote_ctrl->step,
352 remote_ctrl->default_value);
353 }
354
355 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
356 V4L2_CID_VBLANK);
357 if (remote_ctrl) {
358 v4l2_ctrl_g_ctrl(remote_ctrl);
359 __v4l2_ctrl_modify_range(pdata->vblank,
360 remote_ctrl->minimum,
361 remote_ctrl->maximum,
362 remote_ctrl->step,
363 remote_ctrl->default_value);
364 }
365
366 return 0;
367 }
368
rk1608_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)369 static int rk1608_g_frame_interval(struct v4l2_subdev *sd,
370 struct v4l2_subdev_frame_interval *fi)
371 {
372 struct rk1608_dphy *pdata = to_state(sd);
373 struct v4l2_subdev *link_sensor;
374 int ret = 0;
375
376 if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
377 link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
378 if (IS_ERR_OR_NULL(link_sensor)) {
379 dev_err(pdata->dev, "can not get link sensor i2c client\n");
380 return -EINVAL;
381 }
382
383 ret = v4l2_subdev_call(link_sensor,
384 video,
385 g_frame_interval,
386 fi);
387 if (ret)
388 dev_info(pdata->dev, "get link interval fail\n");
389 else
390 return ret;
391 }
392
393 if (!(pdata->rk1608_sd)) {
394 dev_info(pdata->dev, "pdata->rk1608_sd NULL\n");
395 return -EFAULT;
396 }
397 pdata->rk1608_sd->grp_id = sd->grp_id;
398 v4l2_subdev_call(pdata->rk1608_sd,
399 video,
400 g_frame_interval,
401 fi);
402
403 return 0;
404 }
405
rk1608_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)406 static int rk1608_s_frame_interval(struct v4l2_subdev *sd,
407 struct v4l2_subdev_frame_interval *fi)
408 {
409 return 0;
410 }
411
rk1608_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * config)412 static int rk1608_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad_id,
413 struct v4l2_mbus_config *config)
414 {
415
416 struct rk1608_dphy *pdata = to_state(sd);
417 u32 val = 0;
418
419 val = 1 << (pdata->fmt_inf[pdata->fmt_inf_idx].mipi_lane_out - 1) |
420 V4L2_MBUS_CSI2_CHANNEL_0 |
421 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
422
423 config->type = V4L2_MBUS_CSI2_DPHY;
424 config->flags = val;
425
426 return 0;
427 }
428
rk1608_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)429 static long rk1608_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
430 {
431 struct rk1608_dphy *pdata = to_state(sd);
432 struct v4l2_subdev *link_sensor;
433 long ret = 0;
434
435 switch (cmd) {
436 case PREISP_CMD_SAVE_HDRAE_PARAM:
437 ret = v4l2_subdev_call(pdata->rk1608_sd, core, ioctl,
438 cmd, arg);
439 break;
440 case PREISP_CMD_SET_HDRAE_EXP:
441 case RKMODULE_GET_MODULE_INFO:
442 case RKMODULE_AWB_CFG:
443
444 case PREISP_DISP_SET_FRAME_OUTPUT:
445 case PREISP_DISP_SET_FRAME_FORMAT:
446 case PREISP_DISP_SET_FRAME_TYPE:
447 case PREISP_DISP_SET_PRO_TIME:
448 case PREISP_DISP_SET_PRO_CURRENT:
449 case PREISP_DISP_SET_DENOISE:
450 case PREISP_DISP_WRITE_EEPROM:
451 case PREISP_DISP_READ_EEPROM:
452 case PREISP_DISP_SET_LED_ON_OFF:
453 case RKMODULE_SET_QUICK_STREAM:
454 mutex_lock(&rk1608_dphy_mutex);
455 pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
456 ret = v4l2_subdev_call(pdata->rk1608_sd, core, ioctl,
457 cmd, arg);
458 mutex_unlock(&rk1608_dphy_mutex);
459 break;
460 case RKMODULE_GET_CSI_DPHY_PARAM:
461 if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
462 link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
463 if (IS_ERR_OR_NULL(link_sensor)) {
464 dev_err(pdata->dev, "can not get link sensor i2c client\n");
465 return -EINVAL;
466 }
467 ret = v4l2_subdev_call(link_sensor, core, ioctl, cmd, arg);
468 }
469 break;
470 default:
471 ret = -ENOIOCTLCMD;
472 break;
473 }
474 return ret;
475 }
476
477 #ifdef CONFIG_COMPAT
rk1608_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)478 static long rk1608_compat_ioctl32(struct v4l2_subdev *sd,
479 unsigned int cmd, unsigned long arg)
480 {
481 void __user *up = compat_ptr(arg);
482 struct preisp_hdrae_exp_s hdrae_exp;
483 struct rkmodule_inf *inf;
484 struct rkmodule_awb_cfg *cfg;
485 struct rkmodule_csi_dphy_param *dphy_param;
486 u32 stream;
487 long ret = -EFAULT;
488
489 switch (cmd) {
490 case PREISP_CMD_SET_HDRAE_EXP:
491 if (copy_from_user(&hdrae_exp, up, sizeof(hdrae_exp)))
492 return -EFAULT;
493
494 return rk1608_ioctl(sd, cmd, &hdrae_exp);
495 case RKMODULE_GET_MODULE_INFO:
496 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
497 if (!inf) {
498 ret = -ENOMEM;
499 return ret;
500 }
501
502 ret = rk1608_ioctl(sd, cmd, inf);
503 if (!ret)
504 if (copy_to_user(up, inf, sizeof(*inf))) {
505 kfree(inf);
506 return -EFAULT;
507 }
508 kfree(inf);
509 break;
510 case RKMODULE_AWB_CFG:
511 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
512 if (!cfg) {
513 ret = -ENOMEM;
514 return ret;
515 }
516 if (copy_from_user(cfg, up, sizeof(*cfg)))
517 return -EFAULT;
518 ret = rk1608_ioctl(sd, cmd, cfg);
519 kfree(cfg);
520 break;
521 case RKMODULE_SET_QUICK_STREAM:
522 ret = copy_from_user(&stream, up, sizeof(u32));
523 if (!ret)
524 ret = rk1608_ioctl(sd, cmd, &stream);
525 else
526 ret = -EFAULT;
527
528 break;
529 case RKMODULE_GET_CSI_DPHY_PARAM:
530 dphy_param = kzalloc(sizeof(*dphy_param), GFP_KERNEL);
531 if (!dphy_param) {
532 ret = -ENOMEM;
533 return ret;
534 }
535 if (copy_from_user(dphy_param, up, sizeof(*dphy_param)))
536 return -EFAULT;
537 ret = rk1608_ioctl(sd, cmd, dphy_param);
538 kfree(dphy_param);
539 break;
540 default:
541 ret = -ENOIOCTLCMD;
542 break;
543 }
544
545 return ret;
546 }
547 #endif
548
rk1608_g_volatile_ctrl(struct v4l2_ctrl * ctrl)549 static int rk1608_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
550 {
551 struct v4l2_ctrl *remote_ctrl;
552 struct rk1608_dphy *pdata =
553 container_of(ctrl->handler,
554 struct rk1608_dphy, ctrl_handler);
555
556 pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
557 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
558 ctrl->id);
559 if (remote_ctrl) {
560 ctrl->val = v4l2_ctrl_g_ctrl(remote_ctrl);
561 __v4l2_ctrl_modify_range(ctrl,
562 remote_ctrl->minimum,
563 remote_ctrl->maximum,
564 remote_ctrl->step,
565 remote_ctrl->default_value);
566 }
567
568 return 0;
569 }
570
rk1608_set_ctrl(struct v4l2_ctrl * ctrl)571 static int rk1608_set_ctrl(struct v4l2_ctrl *ctrl)
572 {
573 int ret = 0;
574 struct v4l2_ctrl *remote_ctrl;
575 struct rk1608_dphy *pdata =
576 container_of(ctrl->handler,
577 struct rk1608_dphy, ctrl_handler);
578
579 pdata->rk1608_sd->grp_id = pdata->sd.grp_id;
580 remote_ctrl = v4l2_ctrl_find(pdata->rk1608_sd->ctrl_handler,
581 ctrl->id);
582 if (remote_ctrl)
583 ret = v4l2_ctrl_s_ctrl(remote_ctrl, ctrl->val);
584
585 return ret;
586 }
587
588 #define CROP_START(SRC, DST) (((SRC) - (DST)) / 2 / 4 * 4)
rk1608_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)589 static int rk1608_get_selection(struct v4l2_subdev *sd,
590 struct v4l2_subdev_pad_config *cfg,
591 struct v4l2_subdev_selection *sel)
592 {
593 struct rk1608_dphy *pdata = to_state(sd);
594 u32 idx = pdata->fmt_inf_idx;
595 u32 width = pdata->fmt_inf[idx].mf.width;
596 u32 height = pdata->fmt_inf[idx].mf.height;
597 struct v4l2_subdev *link_sensor;
598 int ret = -EINVAL;
599
600 if (sel->target != V4L2_SEL_TGT_CROP_BOUNDS)
601 return -EINVAL;
602
603 if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
604 link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
605 if (IS_ERR_OR_NULL(link_sensor)) {
606 dev_err(pdata->dev, "can not get link sensor i2c client\n");
607 goto err;
608 }
609
610 ret = v4l2_subdev_call(link_sensor, pad, get_selection, NULL, sel);
611 if (!ret)
612 return 0;
613 }
614
615 err:
616 if (pdata->fmt_inf[idx].hcrop && pdata->fmt_inf[idx].vcrop) {
617 width = pdata->fmt_inf[idx].hcrop;
618 height = pdata->fmt_inf[idx].vcrop;
619 }
620
621 sel->r.left = CROP_START(pdata->fmt_inf[idx].mf.width, width);
622 sel->r.top = CROP_START(pdata->fmt_inf[idx].mf.height, height);
623 sel->r.width = width;
624 sel->r.height = height;
625
626 return 0;
627 }
628
rk1608_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)629 static int rk1608_enum_frame_interval(struct v4l2_subdev *sd,
630 struct v4l2_subdev_pad_config *cfg,
631 struct v4l2_subdev_frame_interval_enum *fie)
632 {
633 struct rk1608_dphy *pdata = to_state(sd);
634 u32 idx = pdata->fmt_inf_idx;
635 int ret = 0;
636 struct v4l2_fract max_fps = {
637 .numerator = 10000,
638 .denominator = 300000,
639 };
640 struct v4l2_subdev *link_sensor;
641
642 if (!IS_ERR_OR_NULL(pdata->link_sensor_client)) {
643 link_sensor = i2c_get_clientdata(pdata->link_sensor_client);
644 if (IS_ERR_OR_NULL(link_sensor)) {
645 dev_err(pdata->dev, "can not get link sensor i2c client\n");
646 goto err;
647 }
648
649 ret = v4l2_subdev_call(link_sensor,
650 pad,
651 enum_frame_interval,
652 NULL,
653 fie);
654 return ret;
655 }
656
657 err:
658 if (fie->index >= pdata->fmt_inf_num)
659 return -EINVAL;
660
661 fie->code = pdata->fmt_inf[idx].mf.code;
662 fie->width = pdata->fmt_inf[idx].mf.width;
663 fie->height = pdata->fmt_inf[idx].mf.height;
664 fie->interval = max_fps;
665
666 return ret;
667 }
668
669 static const struct v4l2_ctrl_ops rk1608_ctrl_ops = {
670 .g_volatile_ctrl = rk1608_g_volatile_ctrl,
671 .s_ctrl = rk1608_set_ctrl,
672 };
673
674 static const struct v4l2_ctrl_config rk1608_priv_ctrls[] = {
675 {
676 .ops = NULL,
677 .id = CIFISP_CID_EMB_VC,
678 .type = V4L2_CTRL_TYPE_INTEGER,
679 .name = "Embedded visual channel",
680 .min = 0,
681 .max = 3,
682 .def = 0,
683 .step = 1,
684 }, {
685 .ops = NULL,
686 .id = CIFISP_CID_EMB_DT,
687 .type = V4L2_CTRL_TYPE_INTEGER,
688 .name = "Embedded data type",
689 .min = 0,
690 .max = 0xff,
691 .def = 0x30,
692 .step = 1,
693 }
694 };
695
rk1608_initialize_controls(struct rk1608_dphy * dphy)696 static int rk1608_initialize_controls(struct rk1608_dphy *dphy)
697 {
698 u32 i;
699 int ret;
700 u64 pixel_rate, pixel_bit;
701 u32 idx = dphy->fmt_inf_idx;
702 struct v4l2_ctrl_handler *handler;
703 unsigned long flags = V4L2_CTRL_FLAG_VOLATILE |
704 V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
705
706 handler = &dphy->ctrl_handler;
707 ret = v4l2_ctrl_handler_init(handler, 8);
708 if (ret)
709 return ret;
710
711 dphy->link_freq = v4l2_ctrl_new_int_menu(handler, NULL,
712 V4L2_CID_LINK_FREQ, 0,
713 0, &dphy->link_freqs);
714 if (dphy->link_freq)
715 dphy->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
716
717 switch (dphy->fmt_inf[idx].data_type) {
718 case 0x2b:
719 pixel_bit = 10;
720 break;
721 case 0x2c:
722 pixel_bit = 12;
723 break;
724 default:
725 pixel_bit = 8;
726 break;
727 }
728 pixel_rate = dphy->link_freqs * dphy->fmt_inf[idx].mipi_lane * 2;
729 do_div(pixel_rate, pixel_bit);
730 dphy->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
731 V4L2_CID_PIXEL_RATE,
732 0, pixel_rate, 1, pixel_rate);
733
734 dphy->hblank = v4l2_ctrl_new_std(handler,
735 &rk1608_ctrl_ops,
736 V4L2_CID_HBLANK,
737 0, 0x7FFFFFFF, 1, 0);
738 if (dphy->hblank)
739 dphy->hblank->flags |= flags;
740
741 dphy->vblank = v4l2_ctrl_new_std(handler,
742 &rk1608_ctrl_ops,
743 V4L2_CID_VBLANK,
744 0, 0x7FFFFFFF, 1, 0);
745 if (dphy->vblank)
746 dphy->vblank->flags |= flags;
747
748 dphy->exposure = v4l2_ctrl_new_std(handler,
749 &rk1608_ctrl_ops,
750 V4L2_CID_EXPOSURE,
751 0, 0x7FFFFFFF, 1, 0);
752 if (dphy->exposure)
753 dphy->exposure->flags |= flags;
754
755 dphy->gain = v4l2_ctrl_new_std(handler,
756 &rk1608_ctrl_ops,
757 V4L2_CID_ANALOGUE_GAIN,
758 0, 0x7FFFFFFF, 1, 0);
759 if (dphy->gain)
760 dphy->gain->flags |= flags;
761
762 for (i = 0; i < ARRAY_SIZE(rk1608_priv_ctrls); i++)
763 v4l2_ctrl_new_custom(handler, &rk1608_priv_ctrls[i], NULL);
764
765 if (handler->error) {
766 ret = handler->error;
767 dev_err(dphy->dev,
768 "Failed to init controls(%d)\n", ret);
769 goto err_free_handler;
770 }
771
772 dphy->sd.ctrl_handler = handler;
773
774 return 0;
775
776 err_free_handler:
777 v4l2_ctrl_handler_free(handler);
778
779 return ret;
780 }
781
782 static const struct v4l2_subdev_internal_ops dphy_subdev_internal_ops = {
783 .open = rk1608_s_open,
784 .close = rk1608_s_close,
785 };
786
787 static const struct v4l2_subdev_video_ops rk1608_subdev_video_ops = {
788 .s_stream = rk1608_s_stream,
789 .g_frame_interval = rk1608_g_frame_interval,
790 .s_frame_interval = rk1608_s_frame_interval,
791 };
792
793 static const struct v4l2_subdev_pad_ops rk1608_subdev_pad_ops = {
794 .enum_mbus_code = rk1608_enum_mbus_code,
795 .enum_frame_size = rk1608_enum_frame_sizes,
796 .get_fmt = rk1608_get_fmt,
797 .set_fmt = rk1608_set_fmt,
798 .get_mbus_config = rk1608_g_mbus_config,
799 .get_selection = rk1608_get_selection,
800 .enum_frame_interval = rk1608_enum_frame_interval,
801 };
802
803 static const struct v4l2_subdev_core_ops rk1608_core_ops = {
804 .s_power = rk1608_sensor_power,
805 .ioctl = rk1608_ioctl,
806 #ifdef CONFIG_COMPAT
807 .compat_ioctl32 = rk1608_compat_ioctl32,
808 #endif
809 };
810
811 static const struct v4l2_subdev_ops dphy_subdev_ops = {
812 .core = &rk1608_core_ops,
813 .video = &rk1608_subdev_video_ops,
814 .pad = &rk1608_subdev_pad_ops,
815 };
816
rk1608_dphy_dt_property(struct rk1608_dphy * dphy)817 static int rk1608_dphy_dt_property(struct rk1608_dphy *dphy)
818 {
819 int ret = 0;
820 struct device_node *node = dphy->dev->of_node;
821 struct device_node *parent_node = of_node_get(node);
822 struct device_node *prev_node = NULL;
823 struct i2c_client *link_sensor_client;
824 u32 idx = 0;
825 u32 sub_idx = 0;
826
827 ret = of_property_read_u32(node, "id", &dphy->sd.grp_id);
828 if (ret)
829 dev_warn(dphy->dev, "Can not get id!");
830
831 ret = of_property_read_u32(node, "cam_nums", &dphy->cam_nums);
832 if (ret)
833 dev_warn(dphy->dev, "Can not get cam_nums!");
834
835 ret = of_property_read_u32(node, "in_mipi", &dphy->in_mipi);
836 if (ret)
837 dev_warn(dphy->dev, "Can not get in_mipi!");
838
839 ret = of_property_read_u32(node, "out_mipi", &dphy->out_mipi);
840 if (ret)
841 dev_warn(dphy->dev, "Can not get out_mipi!");
842
843 ret = of_property_read_u64(node, "link-freqs", &dphy->link_freqs);
844 if (ret)
845 dev_warn(dphy->dev, "Can not get link_freqs!");
846
847 ret = of_property_read_u32(node, "sensor_i2c_bus", &dphy->i2c_bus);
848 if (ret)
849 dev_warn(dphy->dev, "Can not get sensor_i2c_bus!");
850
851 ret = of_property_read_u32(node, "sensor_i2c_addr", &dphy->i2c_addr);
852 if (ret)
853 dev_warn(dphy->dev, "Can not get sensor_i2c_addr!");
854
855 ret = of_property_read_string(node, "sensor-name", &dphy->sensor_name);
856 if (ret)
857 dev_warn(dphy->dev, "Can not get sensor-name!");
858
859 node = NULL;
860 while (!IS_ERR_OR_NULL(node =
861 of_get_next_child(parent_node, prev_node))) {
862 if (!strncasecmp(node->name,
863 "format-config",
864 strlen("format-config"))) {
865 ret = of_property_read_u32(node, "data_type",
866 &dphy->fmt_inf[idx].data_type);
867 if (ret)
868 dev_warn(dphy->dev, "Can not get data_type!");
869
870 ret = of_property_read_u32(node, "mipi_lane",
871 &dphy->fmt_inf[idx].mipi_lane);
872 if (ret)
873 dev_warn(dphy->dev, "Can not get mipi_lane!");
874
875 ret = of_property_read_u32(node, "mipi_lane_out",
876 &dphy->fmt_inf[idx].mipi_lane_out);
877 if (ret)
878 dev_warn(dphy->dev, "Can not get mipi_lane_out!");
879
880 ret = of_property_read_u32(node, "field",
881 &dphy->fmt_inf[idx].mf.field);
882 if (ret)
883 dev_warn(dphy->dev, "Can not get field!");
884
885 ret = of_property_read_u32(node, "colorspace",
886 &dphy->fmt_inf[idx].mf.colorspace);
887 if (ret)
888 dev_warn(dphy->dev, "Can not get colorspace!");
889
890 ret = of_property_read_u32(node, "code",
891 &dphy->fmt_inf[idx].mf.code);
892 if (ret)
893 dev_warn(dphy->dev, "Can not get code!");
894
895 ret = of_property_read_u32(node, "width",
896 &dphy->fmt_inf[idx].mf.width);
897 if (ret)
898 dev_warn(dphy->dev, "Can not get width!");
899
900 ret = of_property_read_u32(node, "height",
901 &dphy->fmt_inf[idx].mf.height);
902 if (ret)
903 dev_warn(dphy->dev, "Can not get height!");
904
905 ret = of_property_read_u32(node, "hactive",
906 &dphy->fmt_inf[idx].hactive);
907 if (ret)
908 dev_warn(dphy->dev, "Can not get hactive!");
909
910 ret = of_property_read_u32(node, "vactive",
911 &dphy->fmt_inf[idx].vactive);
912 if (ret)
913 dev_warn(dphy->dev, "Can not get vactive!");
914
915 ret = of_property_read_u32(node, "htotal",
916 &dphy->fmt_inf[idx].htotal);
917 if (ret)
918 dev_warn(dphy->dev, "Can not get htotal!");
919
920 ret = of_property_read_u32(node, "vtotal",
921 &dphy->fmt_inf[idx].vtotal);
922 if (ret)
923 dev_warn(dphy->dev, "Can not get vtotal!");
924
925 ret = of_property_read_u32_array(node, "inch0-info",
926 (u32 *)&dphy->fmt_inf[idx].in_ch[0], 5);
927 if (ret)
928 dev_warn(dphy->dev, "Can not get inch0-info!");
929
930 ret = of_property_read_u32_array(node, "inch1-info",
931 (u32 *)&dphy->fmt_inf[idx].in_ch[1], 5);
932 if (ret)
933 dev_info(dphy->dev, "Can not get inch1-info!");
934
935 ret = of_property_read_u32_array(node, "inch2-info",
936 (u32 *)&dphy->fmt_inf[idx].in_ch[2], 5);
937 if (ret)
938 dev_info(dphy->dev, "Can not get inch2-info!");
939
940 ret = of_property_read_u32_array(node, "inch3-info",
941 (u32 *)&dphy->fmt_inf[idx].in_ch[3], 5);
942 if (ret)
943 dev_info(dphy->dev, "Can not get inch3-info!");
944
945 ret = of_property_read_u32_array(node, "outch0-info",
946 (u32 *)&dphy->fmt_inf[idx].out_ch[0], 5);
947 if (ret)
948 dev_warn(dphy->dev, "Can not get outch0-info!");
949
950 ret = of_property_read_u32_array(node, "outch1-info",
951 (u32 *)&dphy->fmt_inf[idx].out_ch[1], 5);
952 if (ret)
953 dev_info(dphy->dev, "Can not get outch1-info!");
954
955 ret = of_property_read_u32_array(node, "outch2-info",
956 (u32 *)&dphy->fmt_inf[idx].out_ch[2], 5);
957 if (ret)
958 dev_info(dphy->dev, "Can not get outch2-info!");
959
960 ret = of_property_read_u32_array(node, "outch3-info",
961 (u32 *)&dphy->fmt_inf[idx].out_ch[3], 5);
962 if (ret)
963 dev_info(dphy->dev, "Can not get outch3-info!");
964
965 ret = of_property_read_u32(node, "hcrop",
966 &dphy->fmt_inf[idx].hcrop);
967 if (ret)
968 dev_warn(dphy->dev, "Can not get hcrop!");
969
970 ret = of_property_read_u32(node, "vcrop",
971 &dphy->fmt_inf[idx].vcrop);
972 if (ret)
973 dev_warn(dphy->dev, "Can not get vcrop!");
974
975 idx++;
976 }
977
978 of_node_put(prev_node);
979 prev_node = node;
980 }
981 dphy->fmt_inf_num = idx;
982
983 prev_node = NULL;
984 /* get virtual sub sensor */
985 node = NULL;
986 while (!IS_ERR_OR_NULL(node =
987 of_get_next_child(parent_node, prev_node))) {
988 if (!strncasecmp(node->name,
989 "virtual-sub-sensor-config",
990 strlen("virtual-sub-sensor-config"))) {
991
992 if (sub_idx >= 4) {
993 dev_err(dphy->dev, "get too mach sub_sensor node, max 4.\n");
994 break;
995 }
996
997 ret = of_property_read_u32(node, "id",
998 &dphy->sub_sensor[sub_idx].id);
999 if (ret)
1000 dev_warn(dphy->dev, "Can not get sub sensor id!");
1001 else
1002 dev_info(dphy->dev, "get sub sensor id:%d",
1003 dphy->sub_sensor[sub_idx].id);
1004
1005 ret = of_property_read_u32(node, "in_mipi",
1006 &dphy->sub_sensor[sub_idx].in_mipi);
1007 if (ret)
1008 dev_warn(dphy->dev, "Can not get sub sensor in_mipi!");
1009 else
1010 dev_info(dphy->dev, "get sub sensor in_mipi:%d",
1011 dphy->sub_sensor[sub_idx].in_mipi);
1012
1013 ret = of_property_read_u32(node, "out_mipi",
1014 &dphy->sub_sensor[sub_idx].out_mipi);
1015 if (ret)
1016 dev_warn(dphy->dev, "Can not get sub sensor out_mipi!");
1017 else
1018 dev_info(dphy->dev, "get sub sensor out_mipi:%d",
1019 dphy->sub_sensor[sub_idx].out_mipi);
1020
1021 sub_idx++;
1022 }
1023
1024 of_node_put(prev_node);
1025 prev_node = node;
1026 }
1027 dphy->sub_sensor_num = sub_idx;
1028
1029 node = of_parse_phandle(parent_node, "link-sensor", 0);
1030 if (node) {
1031 dev_info(dphy->dev, "get link sensor node:%s\n", node->full_name);
1032 link_sensor_client =
1033 of_find_i2c_device_by_node(node);
1034 of_node_put(node);
1035 if (IS_ERR_OR_NULL(link_sensor_client)) {
1036 dev_err(dphy->dev, "can not get link sensor node\n");
1037 } else {
1038 dphy->link_sensor_client = link_sensor_client;
1039 dev_info(dphy->dev, "get link sensor client\n");
1040 }
1041 } else {
1042 dev_err(dphy->dev, "can not get link-sensor node\n");
1043 }
1044 /* get virtual sub sensor end */
1045
1046 of_node_put(prev_node);
1047 of_node_put(parent_node);
1048
1049 return ret;
1050 }
1051
rk1608_dphy_probe(struct platform_device * pdev)1052 static int rk1608_dphy_probe(struct platform_device *pdev)
1053 {
1054 struct rk1608_dphy *dphy;
1055 struct v4l2_subdev *sd;
1056 struct device_node *node = pdev->dev.of_node;
1057 char facing[2];
1058 int ret = 0;
1059
1060 dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
1061 if (!dphy)
1062 return -ENOMEM;
1063
1064 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1065 &dphy->module_index);
1066 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1067 &dphy->module_facing);
1068 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1069 &dphy->module_name);
1070 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1071 &dphy->len_name);
1072 if (ret) {
1073 dev_err(dphy->dev,
1074 "could not get module information!\n");
1075 return -EINVAL;
1076 }
1077
1078 dphy->dev = &pdev->dev;
1079 platform_set_drvdata(pdev, dphy);
1080 sd = &dphy->sd;
1081 sd->dev = &pdev->dev;
1082 v4l2_subdev_init(sd, &dphy_subdev_ops);
1083 rk1608_dphy_dt_property(dphy);
1084
1085 memset(facing, 0, sizeof(facing));
1086 if (strcmp(dphy->module_facing, "back") == 0)
1087 facing[0] = 'b';
1088 else
1089 facing[0] = 'f';
1090
1091 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s RK1608-dphy%d",
1092 dphy->module_index, facing,
1093 RK1608_DPHY_NAME, sd->grp_id);
1094 rk1608_initialize_controls(dphy);
1095 sd->internal_ops = &dphy_subdev_internal_ops;
1096 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1097 dphy->pad.flags = MEDIA_PAD_FL_SOURCE;
1098 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1099
1100 ret = media_entity_pads_init(&sd->entity, 1, &dphy->pad);
1101 if (ret < 0)
1102 goto handler_err;
1103 ret = v4l2_async_register_subdev_sensor_common(sd);
1104 if (ret < 0)
1105 goto register_err;
1106
1107 dev_info(dphy->dev, "RK1608-dphy(%d) probe success!\n", sd->grp_id);
1108
1109 return 0;
1110 register_err:
1111 media_entity_cleanup(&sd->entity);
1112 handler_err:
1113 v4l2_ctrl_handler_free(dphy->sd.ctrl_handler);
1114 devm_kfree(&pdev->dev, dphy);
1115 return ret;
1116 }
1117
rk1608_dphy_remove(struct platform_device * pdev)1118 static int rk1608_dphy_remove(struct platform_device *pdev)
1119 {
1120 struct rk1608_dphy *dphy = platform_get_drvdata(pdev);
1121
1122 v4l2_async_unregister_subdev(&dphy->sd);
1123 media_entity_cleanup(&dphy->sd.entity);
1124 v4l2_ctrl_handler_free(&dphy->ctrl_handler);
1125
1126 return 0;
1127 }
1128
1129 static const struct of_device_id dphy_of_match[] = {
1130 { .compatible = "rockchip,rk1608-dphy" },
1131 { /* sentinel */ },
1132 };
1133
1134 MODULE_DEVICE_TABLE(of, dphy_of_match);
1135
1136 static struct platform_driver rk1608_dphy_drv = {
1137 .driver = {
1138 .of_match_table = of_match_ptr(dphy_of_match),
1139 .name = RK1608_DPHY_NAME,
1140 },
1141 .probe = rk1608_dphy_probe,
1142 .remove = rk1608_dphy_remove,
1143 };
1144
1145 module_platform_driver(rk1608_dphy_drv);
1146
1147 MODULE_AUTHOR("Rockchip Camera/ISP team");
1148 MODULE_DESCRIPTION("A DSP driver for rk1608 chip");
1149 MODULE_LICENSE("GPL v2");
1150