1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * tp2855 driver
4 *
5 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6 *
7 * V0.0X01.0X00 first version.
8 */
9
10 #define DEBUG
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/sysfs.h>
20 #include <linux/slab.h>
21 #include <linux/version.h>
22 #include <linux/rk-camera-module.h>
23 #include <media/media-entity.h>
24 #include <media/v4l2-async.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-subdev.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/rk-preisp.h>
29 #include <linux/sched.h>
30 #include <linux/kthread.h>
31
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/tlv.h>
37
38 #include <linux/platform_device.h>
39 #include <linux/input.h>
40
41 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x0)
42 #define TP2855_TEST_PATTERN 0
43 #define TP2855_XVCLK_FREQ 27000000
44 #define TP2855_LINK_FREQ_297M (297000000UL >> 1)
45 #define TP2855_LINK_FREQ_594M (594000000UL >> 1)
46 #define TP2855_LANES 4
47 #define TP2855_BITS_PER_SAMPLE 8
48 #define TP2855_NAME "tp2855"
49 #define OF_CAMERA_PINCTRL_STATE_DEFAULT "rockchip,camera_default"
50 #define OF_CAMERA_PINCTRL_STATE_SLEEP "rockchip,camera_sleep"
51
52 enum{
53 CH_1=0,
54 CH_2=1,
55 CH_3=2,
56 CH_4=3,
57 CH_ALL=4,
58 MIPI_PAGE=8,
59 };
60
61 enum{
62 STD_TVI, //TVI
63 STD_HDA, //AHD
64 };
65
66 struct regval {
67 u8 addr;
68 u8 val;
69 };
70
71 struct tp2855_mode {
72 u32 bus_fmt;
73 u32 width;
74 u32 height;
75 struct v4l2_fract max_fps;
76 u32 mipi_freq_idx;
77 u32 bpp;
78 const struct regval *global_reg_list;
79 const struct regval *reg_list;
80 u32 hdr_mode;
81 u32 vc[PAD_MAX];
82 u32 channel_reso[PAD_MAX];
83 };
84
85 struct tp2855 {
86 struct i2c_client *client;
87 struct clk *xvclk;
88 struct gpio_desc *reset_gpio;
89 struct gpio_desc *power_gpio;
90
91 struct pinctrl *pinctrl;
92 struct pinctrl_state *pins_default;
93 struct pinctrl_state *pins_sleep;
94
95 struct v4l2_subdev subdev;
96 struct media_pad pad;
97 struct v4l2_ctrl_handler ctrl_handler;
98 struct v4l2_ctrl *pixel_rate;
99 struct v4l2_ctrl *link_freq;
100 struct mutex mutex;
101 bool power_on;
102 struct tp2855_mode *cur_mode;
103
104 u32 module_index;
105 u32 cfg_num;
106 const char *module_facing;
107 const char *module_name;
108 const char *len_name;
109 bool lost_video_status;
110 struct task_struct *detect_thread;
111
112 int streaming;
113 };
114
115
116 #define to_tp2855(sd) container_of(sd, struct tp2855, subdev)
117
118 static __maybe_unused const struct regval common_setting_297M_720p_25fps_regs[] = {
119 {0x40, 0x04},
120 {0xf5, 0x00},
121 {0x02, 0x42},
122 {0x07, 0xc0},
123 {0x0b, 0xc0},
124 {0x0c, 0x13},
125 {0x0d, 0x50},
126 {0x15, 0x13},
127 {0x16, 0x15},
128 {0x17, 0x00},
129 {0x18, 0x19},
130 {0x19, 0xd0},
131 {0x1a, 0x25},
132 {0x1c, 0x07},
133 {0x1d, 0xbc},
134 {0x20, 0x30},
135 {0x21, 0x84},
136 {0x22, 0x36},
137 {0x23, 0x3c},
138 #if TP2855_TEST_PATTERN
139 {0x2a, 0x3c}, //vi test
140 #endif
141 {0x2b, 0x60},
142 {0x2c, 0x0a},
143 {0x2d, 0x30},
144 {0x2e, 0x70},
145 {0x30, 0x48},
146 {0x31, 0xbb},
147 {0x32, 0x2e},
148 {0x33, 0x90},
149 {0x35, 0x25},
150 {0x38, 0x00},
151 {0x39, 0x18},
152
153 {0x40, 0x08},
154 {0x01, 0xf0},
155 {0x02, 0x01},
156 {0x08, 0x0f},
157 {0x20, 0x44},
158 {0x34, 0xe4},
159 {0x14, 0x44},
160 {0x15, 0x0d},
161 {0x25, 0x04},
162 {0x26, 0x03},
163 {0x27, 0x09},
164 {0x29, 0x02},
165 {0x33, 0x07},
166 {0x33, 0x00},
167 {0x14, 0xc4},
168 {0x14, 0x44},
169
170 // {0x23, 0x02}, //vi test ok
171 // {0x23, 0x00},
172 };
173
174 static __maybe_unused const struct regval common_setting_594M_1080p_25fps_regs[] = {
175 {0x40, 0x04},
176 {0xf5, 0x00},
177 {0x02, 0x40},
178 {0x07, 0xc0},
179 {0x0b, 0xc0},
180 {0x0c, 0x03},
181 {0x0d, 0x50},
182 {0x15, 0x03},
183 {0x16, 0xd2},
184 {0x17, 0x80},
185 {0x18, 0x29},
186 {0x19, 0x38},
187 {0x1a, 0x47},
188 {0x1c, 0x0a},
189 {0x1d, 0x50},
190 {0x20, 0x30},
191 {0x21, 0x84},
192 {0x22, 0x36},
193 {0x23, 0x3c},
194 #if TP2855_TEST_PATTERN
195 {0x2a, 0x3c}, //vi test
196 #endif
197 {0x2b, 0x60},
198 {0x2c, 0x0a},
199 {0x2d, 0x30},
200 {0x2e, 0x70},
201 {0x30, 0x48},
202 {0x31, 0xbb},
203 {0x32, 0x2e},
204 {0x33, 0x90},
205 {0x35, 0x05},
206 {0x38, 0x00},
207 {0x39, 0x1C},
208
209 {0x40, 0x08},
210 {0x01, 0xf0},
211 {0x02, 0x01},
212 {0x08, 0x0f},
213 {0x20, 0x44},
214 {0x34, 0xe4},
215 {0x15, 0x0C},
216 {0x25, 0x08},
217 {0x26, 0x06},
218 {0x27, 0x11},
219 {0x29, 0x0a},
220 {0x33, 0x07},
221 {0x33, 0x00},
222 {0x14, 0x33},
223 {0x14, 0xb3},
224 {0x14, 0x33},
225 // {0x23, 0x02}, //vi test ok
226 // {0x23, 0x00},
227 };
228
229 static struct tp2855_mode supported_modes[] = {
230 {
231 .bus_fmt = MEDIA_BUS_FMT_UYVY8_2X8,
232 .width = 1920,
233 .height = 1080,
234 .max_fps = {
235 .numerator = 10000,
236 .denominator = 250000,
237 },
238 .global_reg_list = common_setting_594M_1080p_25fps_regs,
239 .mipi_freq_idx = 0,
240 .bpp = 8,
241 .vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
242 .vc[PAD1] = V4L2_MBUS_CSI2_CHANNEL_1,
243 .vc[PAD2] = V4L2_MBUS_CSI2_CHANNEL_2,
244 .vc[PAD3] = V4L2_MBUS_CSI2_CHANNEL_3,
245 },
246 {
247 .bus_fmt = MEDIA_BUS_FMT_UYVY8_2X8,
248 .width = 1280,
249 .height = 720,
250 .max_fps = {
251 .numerator = 10000,
252 .denominator = 250000,
253 },
254 .global_reg_list = common_setting_297M_720p_25fps_regs,
255 .mipi_freq_idx = 1,
256 .bpp = 8,
257 .vc[PAD0] = V4L2_MBUS_CSI2_CHANNEL_0,
258 .vc[PAD1] = V4L2_MBUS_CSI2_CHANNEL_1,
259 .vc[PAD2] = V4L2_MBUS_CSI2_CHANNEL_2,
260 .vc[PAD3] = V4L2_MBUS_CSI2_CHANNEL_3,
261 }
262 };
263
264 static const s64 link_freq_items[] = {
265 TP2855_LINK_FREQ_594M,
266 TP2855_LINK_FREQ_297M,
267 };
268
269 /* sensor register write */
tp2855_write_reg(struct i2c_client * client,u8 reg,u8 val)270 static int tp2855_write_reg(struct i2c_client *client, u8 reg, u8 val)
271 {
272 struct i2c_msg msg;
273 u8 buf[2];
274 int ret;
275
276 buf[0] = reg & 0xFF;
277 buf[1] = val;
278
279 msg.addr = client->addr;
280 msg.flags = client->flags;
281 msg.buf = buf;
282 msg.len = sizeof(buf);
283
284 ret = i2c_transfer(client->adapter, &msg, 1);
285 if (ret >= 0) {
286 usleep_range(300, 400);
287 return 0;
288 }
289
290 dev_err(&client->dev,
291 "tp2855 write reg(0x%x val:0x%x) failed !\n", reg, val);
292
293 return ret;
294 }
295
tp2855_write_array(struct i2c_client * client,const struct regval * regs,int size)296 static int tp2855_write_array(struct i2c_client *client,
297 const struct regval *regs, int size)
298 {
299 int i, ret = 0;
300
301 i = 0;
302 while (i < size) {
303 ret = tp2855_write_reg(client, regs[i].addr, regs[i].val);
304 if (ret) {
305 dev_err(&client->dev, "%s failed !\n", __func__);
306 break;
307 }
308 i++;
309 }
310
311 return ret;
312 }
313
314 /* sensor register read */
tp2855_read_reg(struct i2c_client * client,u8 reg,u8 * val)315 static int tp2855_read_reg(struct i2c_client *client, u8 reg, u8 *val)
316 {
317 struct i2c_msg msg[2];
318 u8 buf[1];
319 int ret;
320
321 buf[0] = reg & 0xFF;
322
323 msg[0].addr = client->addr;
324 msg[0].flags = client->flags;
325 msg[0].buf = buf;
326 msg[0].len = sizeof(buf);
327
328 msg[1].addr = client->addr;
329 msg[1].flags = client->flags | I2C_M_RD;
330 msg[1].buf = buf;
331 msg[1].len = 1;
332
333 ret = i2c_transfer(client->adapter, msg, 2);
334 if (ret >= 0) {
335 *val = buf[0];
336 return 0;
337 }
338
339 dev_err(&client->dev, "tp2855 read reg(0x%x) failed !\n", reg);
340
341 return ret;
342 }
343
tp2855_get_reso_dist(const struct tp2855_mode * mode,struct v4l2_mbus_framefmt * framefmt)344 static int tp2855_get_reso_dist(const struct tp2855_mode *mode,
345 struct v4l2_mbus_framefmt *framefmt)
346 {
347 return abs(mode->width - framefmt->width) +
348 abs(mode->height - framefmt->height);
349 }
350
351 static const struct tp2855_mode *
tp2855_find_best_fit(struct tp2855 * tp2855,struct v4l2_subdev_format * fmt)352 tp2855_find_best_fit(struct tp2855 *tp2855,
353 struct v4l2_subdev_format *fmt)
354 {
355 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
356 int dist;
357 int cur_best_fit = 0;
358 int cur_best_fit_dist = -1;
359 unsigned int i;
360
361 for (i = 0; i < tp2855->cfg_num; i++) {
362 dist = tp2855_get_reso_dist(&supported_modes[i], framefmt);
363 if ((cur_best_fit_dist == -1 || dist <= cur_best_fit_dist) &&
364 supported_modes[i].bus_fmt == framefmt->code) {
365 cur_best_fit_dist = dist;
366 cur_best_fit = i;
367 }
368 }
369
370 return &supported_modes[cur_best_fit];
371 }
372
tp2855_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)373 static int tp2855_set_fmt(struct v4l2_subdev *sd,
374 struct v4l2_subdev_pad_config *cfg,
375 struct v4l2_subdev_format *fmt)
376 {
377 struct tp2855 *tp2855 = to_tp2855(sd);
378 const struct tp2855_mode *mode;
379 u64 pixel_rate;
380
381 mutex_lock(&tp2855->mutex);
382
383 mode = tp2855_find_best_fit(tp2855, fmt);
384 fmt->format.code = mode->bus_fmt;
385 fmt->format.width = mode->width;
386 fmt->format.height = mode->height;
387 fmt->format.field = V4L2_FIELD_NONE;
388 fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
389
390 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
391 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
392 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
393 #else
394 mutex_unlock(&tp2855->mutex);
395 return -ENOTTY;
396 #endif
397 } else {
398 __v4l2_ctrl_s_ctrl(tp2855->link_freq, mode->mipi_freq_idx);
399 pixel_rate = (u32)link_freq_items[mode->mipi_freq_idx] / mode->bpp * 2 * TP2855_LANES;
400 __v4l2_ctrl_s_ctrl_int64(tp2855->pixel_rate, pixel_rate);
401 dev_dbg(&tp2855->client->dev, "mipi_freq_idx %d\n", mode->mipi_freq_idx);
402 dev_dbg(&tp2855->client->dev, "pixel_rate %lld\n", pixel_rate);
403 }
404
405 mutex_unlock(&tp2855->mutex);
406 return 0;
407 }
408
tp2855_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)409 static int tp2855_get_fmt(struct v4l2_subdev *sd,
410 struct v4l2_subdev_pad_config *cfg,
411 struct v4l2_subdev_format *fmt)
412 {
413 struct tp2855 *tp2855 = to_tp2855(sd);
414 struct i2c_client *client = tp2855->client;
415 const struct tp2855_mode *mode = tp2855->cur_mode;
416
417 mutex_lock(&tp2855->mutex);
418 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
419 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
420 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
421 #else
422 mutex_unlock(&tp2855->mutex);
423 return -ENOTTY;
424 #endif
425 } else {
426 fmt->format.width = mode->width;
427 fmt->format.height = mode->height;
428 fmt->format.code = mode->bus_fmt;
429 fmt->format.field = V4L2_FIELD_NONE;
430 if (fmt->pad < PAD_MAX && fmt->pad >= PAD0)
431 fmt->reserved[0] = mode->vc[fmt->pad];
432 else
433 fmt->reserved[0] = mode->vc[PAD0];
434 }
435 mutex_unlock(&tp2855->mutex);
436
437 dev_dbg(&client->dev, "%s: %x %dx%d\n",
438 __func__, fmt->format.code,
439 fmt->format.width, fmt->format.height);
440
441 return 0;
442 }
443
444
tp2855_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)445 static int tp2855_enum_mbus_code(struct v4l2_subdev *sd,
446 struct v4l2_subdev_pad_config *cfg,
447 struct v4l2_subdev_mbus_code_enum *code)
448 {
449 struct tp2855 *tp2855 = to_tp2855(sd);
450
451 if (code->index != 0)
452 return -EINVAL;
453 code->code = tp2855->cur_mode->bus_fmt;
454
455 return 0;
456 }
457
tp2855_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)458 static int tp2855_enum_frame_sizes(struct v4l2_subdev *sd,
459 struct v4l2_subdev_pad_config *cfg,
460 struct v4l2_subdev_frame_size_enum *fse)
461 {
462 struct tp2855 *tp2855 = to_tp2855(sd);
463 struct i2c_client *client = tp2855->client;
464
465 dev_dbg(&client->dev, "%s:\n", __func__);
466
467 if (fse->index >= tp2855->cfg_num)
468 return -EINVAL;
469
470 if (fse->code != supported_modes[fse->index].bus_fmt)
471 return -EINVAL;
472
473 fse->min_width = supported_modes[fse->index].width;
474 fse->max_width = supported_modes[fse->index].width;
475 fse->max_height = supported_modes[fse->index].height;
476 fse->min_height = supported_modes[fse->index].height;
477 return 0;
478 }
479
tp2855_g_mbus_config(struct v4l2_subdev * sd,struct v4l2_mbus_config * cfg)480 static int tp2855_g_mbus_config(struct v4l2_subdev *sd,
481 struct v4l2_mbus_config *cfg)
482 {
483 cfg->type = V4L2_MBUS_CSI2;
484 cfg->flags = V4L2_MBUS_CSI2_4_LANE |
485 V4L2_MBUS_CSI2_CHANNELS;
486
487 return 0;
488 }
489
tp2855_get_module_inf(struct tp2855 * tp2855,struct rkmodule_inf * inf)490 static void tp2855_get_module_inf(struct tp2855 *tp2855,
491 struct rkmodule_inf *inf)
492 {
493 memset(inf, 0, sizeof(*inf));
494 strlcpy(inf->base.sensor, TP2855_NAME, sizeof(inf->base.sensor));
495 strlcpy(inf->base.module, tp2855->module_name,
496 sizeof(inf->base.module));
497 strlcpy(inf->base.lens, tp2855->len_name, sizeof(inf->base.lens));
498 }
499
tp2855_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)500 static long tp2855_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
501 {
502 struct tp2855 *tp2855 = to_tp2855(sd);
503 long ret = 0;
504
505 switch (cmd) {
506 case RKMODULE_GET_MODULE_INFO:
507 tp2855_get_module_inf(tp2855, (struct rkmodule_inf *)arg);
508 break;
509 default:
510 ret = -ENOTTY;
511 break;
512 }
513
514 return ret;
515 }
516
517 #ifdef CONFIG_COMPAT
tp2855_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)518 static long tp2855_compat_ioctl32(struct v4l2_subdev *sd,
519 unsigned int cmd, unsigned long arg)
520 {
521 void __user *up = compat_ptr(arg);
522 struct rkmodule_inf *inf;
523 struct rkmodule_awb_cfg *cfg;
524 long ret = 0;
525
526 switch (cmd) {
527 case RKMODULE_GET_MODULE_INFO:
528 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
529 if (!inf) {
530 ret = -ENOMEM;
531 return ret;
532 }
533
534 ret = tp2855_ioctl(sd, cmd, inf);
535 if (!ret)
536 ret = copy_to_user(up, inf, sizeof(*inf));
537 kfree(inf);
538 break;
539 case RKMODULE_AWB_CFG:
540 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
541 if (!cfg) {
542 ret = -ENOMEM;
543 return ret;
544 }
545
546 ret = copy_from_user(cfg, up, sizeof(*cfg));
547 if (!ret)
548 ret = tp2855_ioctl(sd, cmd, cfg);
549 kfree(cfg);
550 break;
551 default:
552 ret = -ENOIOCTLCMD;
553 break;
554 }
555
556 return ret;
557 }
558 #endif
559
detect_thread_function(void * data)560 static int detect_thread_function(void *data)
561 {
562 struct tp2855 *tp2855 = (struct tp2855 *) data;
563 struct i2c_client *client = tp2855->client;
564 u8 detect_status = 0, reg26_val = 0;
565 bool lost_video = false;
566 tp2855->lost_video_status = true;
567 while (!kthread_should_stop()) {
568 if (tp2855->power_on) {
569 tp2855_write_reg(client, 0x40, 0x04);
570 tp2855_read_reg(client, 0x01, &detect_status);
571 tp2855_read_reg(client, 0x26, ®26_val);
572 lost_video = (detect_status & 0x80) ? true : false;
573 if (tp2855->lost_video_status != lost_video) {
574 if (lost_video) {
575 tp2855_write_reg(client, 0x26, (reg26_val & 0xfe));
576 } else {
577 tp2855_write_reg(client, 0x26, (reg26_val | 0x01));
578 }
579 tp2855->lost_video_status = lost_video;
580 tp2855_read_reg(client, 0x26, ®26_val);
581 dev_err(&client->dev, "tp2855 detect video lost status %d reg26_val %x\n",
582 lost_video, reg26_val);
583 }
584 }
585 set_current_state(TASK_INTERRUPTIBLE);
586 schedule_timeout(msecs_to_jiffies(200));
587 }
588 return 0;
589 }
590
detect_thread_start(struct tp2855 * tp2855)591 static int __maybe_unused detect_thread_start(struct tp2855 *tp2855) {
592 int ret = 0;
593 struct i2c_client *client = tp2855->client;
594 tp2855->detect_thread = kthread_create(detect_thread_function,
595 tp2855, "tp2855_kthread");
596 if (IS_ERR(tp2855->detect_thread)) {
597 dev_err(&client->dev, "kthread_create tp2855_kthread failed\n");
598 ret = PTR_ERR(tp2855->detect_thread);
599 tp2855->detect_thread = NULL;
600 return ret;
601 }
602 wake_up_process(tp2855->detect_thread);
603 return ret;
604 }
605
detect_thread_stop(struct tp2855 * tp2855)606 static int __maybe_unused detect_thread_stop(struct tp2855 *tp2855) {
607 if (tp2855->detect_thread)
608 kthread_stop(tp2855->detect_thread);
609 tp2855->detect_thread = NULL;
610 return 0;
611 }
612
__tp2855_start_stream(struct tp2855 * tp2855)613 static int __tp2855_start_stream(struct tp2855 *tp2855)
614 {
615 int ret;
616 int array_size = 0;
617 struct i2c_client *client = tp2855->client;
618
619 if (tp2855->cur_mode->global_reg_list == common_setting_594M_1080p_25fps_regs) {
620 array_size = ARRAY_SIZE(common_setting_594M_1080p_25fps_regs);
621 } else if (tp2855->cur_mode->global_reg_list == common_setting_297M_720p_25fps_regs) {
622 array_size = ARRAY_SIZE(common_setting_297M_720p_25fps_regs);
623 } else {
624 return -1;
625 }
626
627 ret = tp2855_write_array(tp2855->client,
628 tp2855->cur_mode->global_reg_list, array_size);
629 if (ret) {
630 dev_err(&client->dev, "__tp2855_start_stream global_reg_list faild");
631 return ret;
632 }
633
634 detect_thread_start(tp2855);
635
636 return 0;
637 }
638
__tp2855_stop_stream(struct tp2855 * tp2855)639 static int __tp2855_stop_stream(struct tp2855 *tp2855)
640 {
641 struct i2c_client *client = tp2855->client;
642
643 tp2855_write_reg(client, 0x40, 0x08);
644 tp2855_write_reg(client, 0x23, 0x02);
645 detect_thread_stop(tp2855);
646
647 return 0;
648 }
649
tp2855_stream(struct v4l2_subdev * sd,int on)650 static int tp2855_stream(struct v4l2_subdev *sd, int on)
651 {
652 struct tp2855 *tp2855 = to_tp2855(sd);
653 struct i2c_client *client = tp2855->client;
654
655 dev_dbg(&client->dev, "s_stream: %d. %dx%d\n", on,
656 tp2855->cur_mode->width,
657 tp2855->cur_mode->height);
658
659 mutex_lock(&tp2855->mutex);
660 on = !!on;
661 if (tp2855->streaming == on)
662 goto unlock;
663
664 if (on) {
665 __tp2855_start_stream(tp2855);
666 } else {
667 __tp2855_stop_stream(tp2855);
668 }
669
670 tp2855->streaming = on;
671
672 unlock:
673 mutex_unlock(&tp2855->mutex);
674
675 return 0;
676 }
677
tp2855_power(struct v4l2_subdev * sd,int on)678 static int tp2855_power(struct v4l2_subdev *sd, int on)
679 {
680 struct tp2855 *tp2855 = to_tp2855(sd);
681 struct i2c_client *client = tp2855->client;
682 int ret = 0;
683
684 mutex_lock(&tp2855->mutex);
685
686 /* If the power state is not modified - no work to do. */
687 if (tp2855->power_on == !!on)
688 goto exit;
689
690 dev_dbg(&client->dev, "%s: on %d\n", __func__, on);
691
692 if (on) {
693 ret = pm_runtime_get_sync(&client->dev);
694 if (ret < 0) {
695 pm_runtime_put_noidle(&client->dev);
696 goto exit;
697 }
698 tp2855->power_on = true;
699 } else {
700 pm_runtime_put(&client->dev);
701 tp2855->power_on = false;
702 }
703
704 exit:
705 mutex_unlock(&tp2855->mutex);
706
707 return ret;
708 }
709
__tp2855_power_on(struct tp2855 * tp2855)710 static int __tp2855_power_on(struct tp2855 *tp2855)
711 {
712 int ret;
713 struct device *dev = &tp2855->client->dev;
714
715 dev_dbg(dev, "%s\n", __func__);
716
717 if (!IS_ERR_OR_NULL(tp2855->pins_default)) {
718 ret = pinctrl_select_state(tp2855->pinctrl,
719 tp2855->pins_default);
720 if (ret < 0)
721 dev_err(dev, "could not set pins. ret=%d\n", ret);
722 }
723
724 if (!IS_ERR(tp2855->power_gpio)) {
725 gpiod_set_value_cansleep(tp2855->power_gpio, 1);
726 usleep_range(25*1000, 30*1000);
727 }
728
729 usleep_range(1500, 2000);
730
731 ret = clk_set_rate(tp2855->xvclk, TP2855_XVCLK_FREQ);
732 if (ret < 0)
733 dev_warn(dev, "Failed to set xvclk rate\n");
734 if (clk_get_rate(tp2855->xvclk) != TP2855_XVCLK_FREQ)
735 dev_warn(dev, "xvclk mismatched\n");
736 ret = clk_prepare_enable(tp2855->xvclk);
737 if (ret < 0) {
738 dev_err(dev, "Failed to enable xvclk\n");
739 goto err_clk;
740 }
741
742 if (!IS_ERR(tp2855->reset_gpio)) {
743 gpiod_set_value_cansleep(tp2855->reset_gpio, 1);
744 usleep_range(10*1000, 20*1000);
745 gpiod_set_value_cansleep(tp2855->reset_gpio, 0);
746 usleep_range(10*1000, 20*1000);
747 }
748
749 usleep_range(10*1000, 20*1000);
750
751 return 0;
752
753 err_clk:
754 if (!IS_ERR(tp2855->reset_gpio))
755 gpiod_set_value_cansleep(tp2855->reset_gpio, 0);
756
757 if (!IS_ERR_OR_NULL(tp2855->pins_sleep))
758 pinctrl_select_state(tp2855->pinctrl, tp2855->pins_sleep);
759
760 return ret;
761 }
762
__tp2855_power_off(struct tp2855 * tp2855)763 static void __tp2855_power_off(struct tp2855 *tp2855)
764 {
765 int ret;
766 struct device *dev = &tp2855->client->dev;
767
768 dev_dbg(dev, "%s\n", __func__);
769
770 if (!IS_ERR(tp2855->reset_gpio))
771 gpiod_set_value_cansleep(tp2855->reset_gpio, 0);
772 clk_disable_unprepare(tp2855->xvclk);
773
774 if (!IS_ERR_OR_NULL(tp2855->pins_sleep)) {
775 ret = pinctrl_select_state(tp2855->pinctrl,
776 tp2855->pins_sleep);
777 if (ret < 0)
778 dev_dbg(dev, "could not set pins\n");
779 }
780
781 if (!IS_ERR(tp2855->power_gpio))
782 gpiod_set_value_cansleep(tp2855->power_gpio, 0);
783 }
784
tp2855_initialize_controls(struct tp2855 * tp2855)785 static int tp2855_initialize_controls(struct tp2855 *tp2855)
786 {
787 const struct tp2855_mode *mode;
788 struct v4l2_ctrl_handler *handler;
789 u64 pixel_rate;
790 int ret;
791
792 handler = &tp2855->ctrl_handler;
793 mode = tp2855->cur_mode;
794 ret = v4l2_ctrl_handler_init(handler, 2);
795 if (ret)
796 return ret;
797 handler->lock = &tp2855->mutex;
798
799 tp2855->link_freq = v4l2_ctrl_new_int_menu(handler, NULL,
800 V4L2_CID_LINK_FREQ,
801 ARRAY_SIZE(link_freq_items) - 1, 0,
802 link_freq_items);
803 __v4l2_ctrl_s_ctrl(tp2855->link_freq, mode->mipi_freq_idx);
804
805 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
806 pixel_rate = (u32)link_freq_items[mode->mipi_freq_idx] / mode->bpp * 2 * TP2855_LANES;
807 tp2855->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
808 V4L2_CID_PIXEL_RATE, 0, pixel_rate,
809 1, pixel_rate);
810
811
812 if (handler->error) {
813 ret = handler->error;
814 dev_err(&tp2855->client->dev,
815 "Failed to init controls(%d)\n", ret);
816 goto err_free_handler;
817 }
818
819 tp2855->subdev.ctrl_handler = handler;
820
821 return 0;
822
823 err_free_handler:
824 v4l2_ctrl_handler_free(handler);
825
826 return ret;
827 }
828
tp2855_runtime_resume(struct device * dev)829 static int tp2855_runtime_resume(struct device *dev)
830 {
831 struct i2c_client *client = to_i2c_client(dev);
832 struct v4l2_subdev *sd = i2c_get_clientdata(client);
833 struct tp2855 *tp2855 = to_tp2855(sd);
834
835 return __tp2855_power_on(tp2855);
836 }
837
tp2855_runtime_suspend(struct device * dev)838 static int tp2855_runtime_suspend(struct device *dev)
839 {
840 struct i2c_client *client = to_i2c_client(dev);
841 struct v4l2_subdev *sd = i2c_get_clientdata(client);
842 struct tp2855 *tp2855 = to_tp2855(sd);
843
844 __tp2855_power_off(tp2855);
845
846 return 0;
847 }
848
849 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
tp2855_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)850 static int tp2855_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
851 {
852 struct tp2855 *tp2855 = to_tp2855(sd);
853 struct v4l2_mbus_framefmt *try_fmt =
854 v4l2_subdev_get_try_format(sd, fh->pad, 0);
855 const struct tp2855_mode *def_mode = &supported_modes[0];
856
857 dev_dbg(&tp2855->client->dev, "%s\n", __func__);
858
859 mutex_lock(&tp2855->mutex);
860 /* Initialize try_fmt */
861 try_fmt->width = def_mode->width;
862 try_fmt->height = def_mode->height;
863 try_fmt->code = def_mode->bus_fmt;
864 try_fmt->field = V4L2_FIELD_NONE;
865
866 mutex_unlock(&tp2855->mutex);
867 /* No crop or compose */
868
869 return 0;
870 }
871 #endif
872
873 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
874 static const struct v4l2_subdev_internal_ops tp2855_internal_ops = {
875 .open = tp2855_open,
876 };
877 #endif
878
879 static const struct v4l2_subdev_video_ops tp2855_video_ops = {
880 .s_stream = tp2855_stream,
881 .g_mbus_config = tp2855_g_mbus_config,
882 };
883
884 static const struct v4l2_subdev_pad_ops tp2855_subdev_pad_ops = {
885 .enum_mbus_code = tp2855_enum_mbus_code,
886 .enum_frame_size = tp2855_enum_frame_sizes,
887 .get_fmt = tp2855_get_fmt,
888 .set_fmt = tp2855_set_fmt,
889 };
890
891 static const struct v4l2_subdev_core_ops tp2855_core_ops = {
892 .s_power = tp2855_power,
893 .ioctl = tp2855_ioctl,
894 #ifdef CONFIG_COMPAT
895 .compat_ioctl32 = tp2855_compat_ioctl32,
896 #endif
897 };
898
899 static const struct v4l2_subdev_ops tp2855_subdev_ops = {
900 .core = &tp2855_core_ops,
901 .video = &tp2855_video_ops,
902 .pad = &tp2855_subdev_pad_ops,
903 };
904
check_chip_id(struct i2c_client * client)905 static int check_chip_id(struct i2c_client *client){
906 struct device *dev = &client->dev;
907 unsigned char chip_id = 0xFF;
908 tp2855_read_reg(client, 0x34, &chip_id);
909 dev_err(dev, "chip_id : 0x%2x\n", chip_id);
910 if (chip_id != 0x0) {
911 return -1;
912 }
913 return 0;
914 }
915
tp2855_probe(struct i2c_client * client,const struct i2c_device_id * id)916 static int tp2855_probe(struct i2c_client *client,
917 const struct i2c_device_id *id)
918 {
919 struct device *dev = &client->dev;
920 struct device_node *node = dev->of_node;
921 struct tp2855 *tp2855;
922 struct v4l2_subdev *sd;
923 __maybe_unused char facing[2];
924 int ret;
925
926 dev_info(dev, "driver version: %02x.%02x.%02x",
927 DRIVER_VERSION >> 16,
928 (DRIVER_VERSION & 0xff00) >> 8,
929 DRIVER_VERSION & 0x00ff);
930
931 tp2855 = devm_kzalloc(dev, sizeof(*tp2855), GFP_KERNEL);
932 if (!tp2855)
933 return -ENOMEM;
934
935 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
936 &tp2855->module_index);
937 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
938 &tp2855->module_facing);
939 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
940 &tp2855->module_name);
941 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
942 &tp2855->len_name);
943 if (ret) {
944 dev_err(dev, "could not get %s!\n", RKMODULE_CAMERA_LENS_NAME);
945 return -EINVAL;
946 }
947
948 tp2855->client = client;
949 tp2855->cur_mode = &supported_modes[0];
950
951 tp2855->xvclk = devm_clk_get(dev, "xvclk");
952 if (IS_ERR(tp2855->xvclk)) {
953 dev_err(dev, "Failed to get xvclk\n");
954 return -EINVAL;
955 }
956
957 tp2855->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
958 if (IS_ERR(tp2855->reset_gpio))
959 dev_warn(dev, "Failed to get reset-gpios\n");
960
961 tp2855->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
962 if (IS_ERR(tp2855->power_gpio))
963 dev_warn(dev, "Failed to get power-gpios\n");
964
965 tp2855->pinctrl = devm_pinctrl_get(dev);
966 if (!IS_ERR(tp2855->pinctrl)) {
967 tp2855->pins_default =
968 pinctrl_lookup_state(tp2855->pinctrl,
969 OF_CAMERA_PINCTRL_STATE_DEFAULT);
970 if (IS_ERR(tp2855->pins_default))
971 dev_info(dev, "could not get default pinstate\n");
972
973 tp2855->pins_sleep =
974 pinctrl_lookup_state(tp2855->pinctrl,
975 OF_CAMERA_PINCTRL_STATE_SLEEP);
976 if (IS_ERR(tp2855->pins_sleep))
977 dev_info(dev, "could not get sleep pinstate\n");
978 } else {
979 dev_info(dev, "no pinctrl\n");
980 }
981
982 mutex_init(&tp2855->mutex);
983
984 sd = &tp2855->subdev;
985 v4l2_i2c_subdev_init(sd, client, &tp2855_subdev_ops);
986 ret = tp2855_initialize_controls(tp2855);
987 if (ret) {
988 dev_err(dev, "Failed to initialize controls tp2855\n");
989 goto err_destroy_mutex;
990 }
991
992 ret = __tp2855_power_on(tp2855);
993 if (ret) {
994 dev_err(dev, "Failed to power on tp2855\n");
995 goto err_free_handler;
996 }
997
998 ret = check_chip_id(client);
999 if (ret) {
1000 dev_err(dev, "Failed to check senosr id\n");
1001 goto err_free_handler;
1002 }
1003
1004 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1005 sd->internal_ops = &tp2855_internal_ops;
1006 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1007 #endif
1008
1009 #if defined(CONFIG_MEDIA_CONTROLLER)
1010 tp2855->pad.flags = MEDIA_PAD_FL_SOURCE;
1011 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1012 ret = media_entity_pads_init(&sd->entity, 1, &tp2855->pad);
1013 if (ret < 0)
1014 goto err_power_off;
1015 #endif
1016
1017 memset(facing, 0, sizeof(facing));
1018 if (strcmp(tp2855->module_facing, "back") == 0)
1019 facing[0] = 'b';
1020 else
1021 facing[0] = 'f';
1022
1023 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1024 tp2855->module_index, facing,
1025 TP2855_NAME, dev_name(sd->dev));
1026
1027 ret = v4l2_async_register_subdev_sensor_common(sd);
1028 if (ret) {
1029 dev_err(dev, "v4l2 async register subdev failed\n");
1030 goto err_clean_entity;
1031 }
1032
1033 pm_runtime_set_active(dev);
1034 pm_runtime_enable(dev);
1035 pm_runtime_idle(dev);
1036
1037 return 0;
1038
1039 err_clean_entity:
1040 #if defined(CONFIG_MEDIA_CONTROLLER)
1041 media_entity_cleanup(&sd->entity);
1042 #endif
1043 err_power_off:
1044 __tp2855_power_off(tp2855);
1045 err_free_handler:
1046 v4l2_ctrl_handler_free(&tp2855->ctrl_handler);
1047 err_destroy_mutex:
1048 mutex_destroy(&tp2855->mutex);
1049
1050 return ret;
1051 }
1052
tp2855_remove(struct i2c_client * client)1053 static int tp2855_remove(struct i2c_client *client)
1054 {
1055 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1056 struct tp2855 *tp2855 = to_tp2855(sd);
1057
1058 v4l2_async_unregister_subdev(sd);
1059 #if defined(CONFIG_MEDIA_CONTROLLER)
1060 media_entity_cleanup(&sd->entity);
1061 #endif
1062 v4l2_ctrl_handler_free(&tp2855->ctrl_handler);
1063 mutex_destroy(&tp2855->mutex);
1064
1065 pm_runtime_disable(&client->dev);
1066 if (!pm_runtime_status_suspended(&client->dev))
1067 __tp2855_power_off(tp2855);
1068 pm_runtime_set_suspended(&client->dev);
1069
1070 return 0;
1071 }
1072
1073 static const struct dev_pm_ops tp2855_pm_ops = {
1074 SET_RUNTIME_PM_OPS(tp2855_runtime_suspend,
1075 tp2855_runtime_resume, NULL)
1076 };
1077
1078 #if IS_ENABLED(CONFIG_OF)
1079 static const struct of_device_id tp2855_of_match[] = {
1080 { .compatible = "tp2855" },
1081 {},
1082 };
1083 MODULE_DEVICE_TABLE(of, tp2855_of_match);
1084 #endif
1085
1086 static const struct i2c_device_id tp2855_match_id[] = {
1087 { "tp2855", 0 },
1088 { },
1089 };
1090
1091 static struct i2c_driver tp2855_i2c_driver = {
1092 .driver = {
1093 .name = TP2855_NAME,
1094 .pm = &tp2855_pm_ops,
1095 .of_match_table = of_match_ptr(tp2855_of_match),
1096 },
1097 .probe = &tp2855_probe,
1098 .remove = &tp2855_remove,
1099 .id_table = tp2855_match_id,
1100 };
1101
sensor_mod_init(void)1102 static int __init sensor_mod_init(void)
1103 {
1104 return i2c_add_driver(&tp2855_i2c_driver);
1105 }
1106
sensor_mod_exit(void)1107 static void __exit sensor_mod_exit(void)
1108 {
1109 i2c_del_driver(&tp2855_i2c_driver);
1110 }
1111
1112 device_initcall_sync(sensor_mod_init);
1113 module_exit(sensor_mod_exit);
1114
1115 MODULE_AUTHOR("Vicent Chi <vicent.chi@rock-chips.com>");
1116 MODULE_DESCRIPTION("tp2855 sensor driver");
1117 MODULE_LICENSE("GPL v2");