hc
2023-11-22 f743a7adbd6e230d66a6206fa115b59fec2d88eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* SPDX-License-Identifier: GPL-2.0 */
/**
 * Rockchip rk1608 driver
 *
 * Copyright (C) 2017-2018 Rockchip Electronics Co., Ltd.
 *
 */
 
#ifndef __RK1608_CORE_H__
#define __RK1608_CORE_H__
 
#include <linux/clk.h>
#include <linux/spi/spi.h>
#include <linux/miscdevice.h>
#include <linux/version.h>
#include "rk1608_dphy.h"
 
#define RK1608_VERSION            KERNEL_VERSION(0, 0x01, 0x04)
 
#define UPDATE_REF_DATA_FROM_EEPROM (1)
 
#define RK1608_OP_TRY_MAX        3
#define RK1608_OP_TRY_DELAY        10
#define RK1608_CMD_WRITE        0x00000011
#define RK1608_CMD_WRITE_REG0        0X00010011
#define RK1608_CMD_WRITE_REG1        0X00020011
#define RK1608_CMD_READ            0x00000077
#define RK1608_CMD_READ_BEGIN        0x000000aa
#define RK1608_CMD_QUERY        0x000000ff
#define RK1608_CMD_QUERY_REG2        0x000001ff
#define RK1608_STATE_ID_MASK        0xffff0000
#define RK1608_STATE_ID            0X16080000
#define RK1608_STATE_MASK        0x0000ffff
#define APB_CMD_WRITE_REG1        0X00020011
#define RK1608_R_MSG_QUEUE_ADDR        0x60050000
 
#define RK1608_IRQ_TYPE_MSG        0x12345678
#define BOOT_REQUEST_ADDR        0x18000010
#define RK1608_HEAD_ADDR        0x60000000
#define RK1608_FW_NAME            "rk1608.rkl"
#define RK1608_S_MSG_QUEUE_ADDR        0x60050010
#define RK1608_PMU_SYS_REG0        0x120000f0
#define RK1608_MSG_QUEUE_OK_MASK    0xffff0001
#define RK1608_MSG_QUEUE_OK_TAG        0x16080001
#define RK1608_MAX_OP_BYTES        60000
#define MSG_SYNC_TIMEOUT        3000
 
#define BOOT_FLAG_CRC            (0x01 << 0)
#define BOOT_FLAG_EXE            (0x01 << 1)
#define BOOT_FLAG_LOAD_PMEM        (0x01 << 2)
#define BOOT_FLAG_ACK            (0x01 << 3)
#define BOOT_FLAG_READ_WAIT        (0x01 << 4)
#define BOOT_FLAG_BOOT_REQUEST        (0x01 << 5)
 
#define DEBUG_DUMP_ALL_SEND_RECV_MSG    0
#define RK1608_MCLK_RATE        (24 * 1000 * 1000ul)
#define SENSOR_TIMEOUT            1000
 
#define OPM_SLAVE_MODE            0x100000
#define RSD_SEL_2CYC            0x008000
#define DFS_SEL_16BIT            0x000002
#define SPI_CTRL0            0x11060000
#define SPI_ENR                0x11060008
#define CRUPMU_CLKSEL14_CON        0x12008098
#define PMUGRF_GPIO1A_E            0x12030040
#define PMUGRF_GPIO1B_E            0x12030044
#define BIT7_6_SEL_8MA            0xf000a000
#define BIT1_0_SEL_8MA            0x000f000a
#define SPI0_PLL_SEL_APLL        0xff004000
#define INVALID_ID            -1
#define RK1608_MAX_SEC_NUM        10
 
#define ISP_DSP_HDRAE_MAXGRIDITEMS    225
 
#define MIRROR_BIT_MASK BIT(0)
#define FLIP_BIT_MASK BIT(1)
 
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
 
#ifndef MSB2LSB32
#define MSB2LSB32(x)    ((((u32)(x) & 0x80808080) >> 7) | \
           (((u32)(x) & 0x40404040) >> 5) | \
           (((u32)(x) & 0x20202020) >> 3) | \
           (((u32)(x) & 0x10101010) >> 1) | \
           (((u32)(x) & 0x08080808) << 1) | \
           (((u32)(x) & 0x04040404) << 3) | \
           (((u32)(x) & 0x02020202) << 5) | \
           (((u32)(x) & 0x01010101) << 7))
#endif
 
struct rk1608_client_list {
   struct mutex mutex; /* protect clients */
   struct list_head list;
};
 
struct rk1608_state {
   struct v4l2_subdev sd;
   struct rk1608_dphy *dphy[2];
   struct mutex lock; /* protect resource */
   struct mutex sensor_lock; /* protect sensor */
   struct mutex send_msg_lock; /* protect msg */
   struct mutex spi2apb_lock; /* protect spi2apb write/read */
   spinlock_t hdrae_lock; /* protect hdrae parameter */
   struct gpio_desc *reset_gpio;
   struct gpio_desc *pwren_gpio;
   struct gpio_desc *irq_gpio;
   int irq;
   struct gpio_desc *wakeup_gpio;
   struct gpio_desc *aesync_gpio;
   struct v4l2_subdev *sensor[4];
   struct device *dev;
   struct spi_device *spi;
   struct clk *mclk;
   struct miscdevice misc;
   struct rk1608_client_list clients;
   int log_level;
   int power_count;
   int msg_num;
   u32 link_nums;
   u32 sensor_cnt;
   u32 sensor_nums[2];
   u32 msg_done[8];
   wait_queue_head_t msg_wait;
   struct media_pad pad;
   struct v4l2_ctrl *link_freq;
   struct v4l2_ctrl *pixel_rate;
   struct v4l2_ctrl *hblank;
   struct v4l2_ctrl *vblank;
   struct v4l2_ctrl *exposure;
   struct v4l2_ctrl *gain;
   struct v4l2_ctrl *h_flip;
   struct v4l2_ctrl *v_flip;
   struct v4l2_ctrl_handler ctrl_handler;
   u32 max_speed_hz;
   u32 min_speed_hz;
   struct preisp_hdrae_para_s hdrae_para;
   struct preisp_hdrae_exp_s hdrae_exp;
   u32 set_exp_cnt;
   const char *firm_name;
   u8 flip;
};
 
struct rk1608_section {
   union {
       u32 offset;
       u32 wait_value;
   };
   u32 size;
   union {
       u32 load_addr;
       u32 wait_addr;
   };
   u16 wait_time;
   u16 timeout;
   u16 crc_16;
   u8 flag;
   u8 type;
};
 
struct rk1608_header {
   char version[32];
   u32 header_size;
   u32 section_count;
   struct rk1608_section sections[RK1608_MAX_SEC_NUM];
};
 
struct rk1608_boot_req {
   u32 flag;
   u32 load_addr;
   u32 boot_len;
   u8 status;
   u8 dummy[2];
   u8 cmd;
};
 
struct rk1608_msg_queue {
   u32 buf_head; /* msg buffer head */
   u32 buf_tail; /* msg buffer tail */
   u32 cur_send; /* current msg send position */
   u32 cur_recv; /* current msg receive position */
};
 
enum _log_level {
   LOG_ERROR,
   LOG_WARN,
   LOG_INFO,
   LOG_DEBUG,
};
 
struct msg {
   u32 size; /* unit 4 bytes */
   u16 type;
   union {
       u8 camera_id;
       u8 core_id;
   } id;
   union {
       u8 sync;
       u8 log_level;
       s8 err;
   } mux;
};
 
struct msg_init {
   struct msg msg_head;
   u32 i2c_bus;
   u32 i2c_clk;
   s8 in_mipi_phy;
   s8 out_mipi_phy;
   s8 mipi_lane;
   s8 bayer;
   u8 sensor_name[32];
   u8 i2c_slave_addr;
};
 
struct preisp_vc_cfg {
   s8 data_id;
   s8 decode_format;
   s8 flag;
   s8 unused;
 
   u16 width;
   u16 height;
};
 
struct msg_in_size {
   struct msg msg_head;
   struct preisp_vc_cfg channel[4];
};
 
struct msg_out_size_head {
   struct msg msg_head;
   u16 width;
   u16 height;
   u32 mipi_clk;
   u16 line_length_pclk;
   u16 frame_length_lines;
   u16 mipi_lane;
   union {
       u16 flip;
       u16 reserved;
   };
};
 
struct msg_set_output_size {
   struct msg_out_size_head head;
   struct preisp_vc_cfg channel[4];
};
 
struct msg_init_dsp_time {
   struct msg msg_head;
   u16 t_ms;
   s32 tv_sec;
   s32 tv_usec;
};
 
enum ISP_AE_Bayer_Mode_e {
   BAYER_MODE_MIN = 0,
   BAYER_MODE_BGGR = 1,
   BAYER_MODE_GRBG = 2,
   BAYER_MODE_GBRG = 3,
   BAYER_MODE_RGGB = 4,
   BAYER_MODE_MAX = 5
};
 
struct ISP_AE_YCOEFF_s {
   int rcoef;
   int gcoef;
   int bcoef;
   int offset;
};
 
enum ISP_AE_Hist_Mode_e {
   AE_HISTSTATICMODE_INVALID = 0,
   AE_HISTSTATICMODE_Y,
   AE_HISTSTATICMODE_R,
   AE_HISTSTATICMODE_G,
   AE_HISTSTATICMODE_B,
   AE_HISTSTATICMODE_MAX
};
 
enum ISP_AE_Grid_Mode_e {
   AE_MEASURE_GRID_INVALID = 0,
   AE_MEASURE_GRID_1X1,
   AE_MEASURE_GRID_5X5,
   AE_MEASURE_GRID_9X9,
   AE_MEASURE_GRID_15X15,
   AE_MEASURE_GRID_MAX
};
 
struct ISP_DSP_hdrae_cfg_s {
   enum ISP_AE_Bayer_Mode_e bayer_mode;
   enum ISP_AE_Grid_Mode_e grid_mode;
   u8 weight[ISP_DSP_HDRAE_MAXGRIDITEMS];
   enum ISP_AE_Hist_Mode_e hist_mode;
   struct ISP_AE_YCOEFF_s ycoeff;
   u8 imgbits;
   u16 width;
   u16 height;
   u16 frames;
};
 
struct msg_set_sensor_info_s {
   struct msg msg_head;
   u32 set_exp_cnt;
   u16 r_gain;
   u16 b_gain;
   u16 gr_gain;
   u16 gb_gain;
   u32 exp_time[3];
   u32 exp_gain[3];
   u32 reg_exp_time[3];
   u32 reg_exp_gain[3];
   s32 lsc_table[17 * 17];
   struct ISP_DSP_hdrae_cfg_s dsp_hdrae;
};
 
struct msg_calib_temp {
   u32 size; // unit 4 bytes
   u16 type; // msg identification
   s8  camera_id;
   s8  sync;
 
   u32 temp;
   u32 calib_version;
 
#if UPDATE_REF_DATA_FROM_EEPROM
   u32 calib_exist;
   u32 calib_sn_size;
   u32 calib_sn_offset;
   u32 calib_sn_code;
#endif
};
 
#define MSG_RESPONSE_ID_OFFSET 0x2ff
 
enum {
   /* AP -> RK1608
    * 1 msg of sensor
    */
   id_msg_init_sensor_t =        0x0001,
   id_msg_set_input_size_t,
   id_msg_set_output_size_t,
   id_msg_set_stream_in_on_t,
   id_msg_set_stream_in_off_t,
   id_msg_set_stream_out_on_t,
   id_msg_set_stream_out_off_t,
 
   /* AP -> RK1608
    * 2 msg of take picture
    */
   id_msg_take_picture_t =        0x0021,
   id_msg_take_picture_done_t,
 
   /* AP -> RK1608
    * 3 msg of realtime parameter
    */
   id_msg_rt_args_t =        0x0031,
   id_msg_set_sensor_info_t,
 
   /* AP -> RK1608
    * 4 msg of power manager
    */
   id_msg_set_sys_mode_bypass_t =    0x0200,
   id_msg_set_sys_mode_standby_t,
   id_msg_set_sys_mode_idle_enable_t,
   id_msg_set_sys_mode_idle_disable_t,
   id_msg_set_sys_mode_slave_dsp_on_t,
   id_msg_set_sys_mode_slave_dsp_off_t,
 
   /* AP -> RK1608
    * 5 msg of debug config
    */
   id_msg_set_log_level_t =    0x0250,
 
   /* RK1608 -> AP
    * 6 response of sensor msg
    */
   id_msg_init_sensor_ret_t =    0x0300,
   id_msg_set_input_size_ret_t,
   id_msg_set_output_size_ret_t,
   id_msg_set_stream_in_on_ret_t,
   id_msg_set_stream_in_off_ret_t,
   id_msg_set_stream_out_on_ret_t,
   id_msg_set_stream_out_off_ret_t,
 
   /* RK1608 -> AP
    * 7 response of take picture msg
    */
   id_msg_take_picture_ret_t =    0x0320,
   id_msg_take_picture_done_ret_t,
 
   /* RK1608 -> AP
    * 8 response of realtime parameter msg
    */
   id_msg_rt_args_ret_t =        0x0330,
 
   /* rk1608 -> AP */
   id_msg_do_i2c_t =        0x0390,
   /* AP -> rk1608 */
   id_msg_do_i2c_ret_t,
 
   /* RK1608 -> AP
    * 9 msg of print log
    */
   id_msg_rk1608_log_t =        0x0400,
 
   /* dsi2csi dump */
   id_msg_dsi2sci_rgb_dump_t =    0x6000,
   id_msg_dsi2sci_nv12_dump_t =    0x6001,
 
   /* RK1608 -> AP
    * 10  msg of xfile
    */
   /* id_msg_xfile_import_t =        0x8000 + 0x0600,
    * id_msg_xfile_export_t,
    * id_msg_xfile_mkdir_t
    */
 
   /* for dsp time. */
   id_msg_frame_time_t = 0x1000,
   id_msg_sys_time_set_t,
 
   //calib temperature and version
   id_msg_temperature_t = 0x1002,
   id_msg_temperature_req_t = 0x1302,
   id_msg_calib_temperature_t = 0x1004,
   id_msg_calib_temperature_req_t = 0x1303,
 
   id_msg_calibration_write_req_t = 0x1050,
   id_msg_calibration_write_done_t,
   id_msg_calibration_read_req_t = 0x1052,
   id_msg_calibration_read_done_t,
   id_msg_calibration_write_req_mode2_t = 0x1054,
   id_msg_calibration_read_req_mode2_t,
 
   id_msg_calibration_write_req_ret_t = 0x1050 + MSG_RESPONSE_ID_OFFSET,
   id_msg_calibration_write_done_ret_t = 0x1051 + MSG_RESPONSE_ID_OFFSET,
   id_msg_calibration_read_req_ret_t = 0x1052 + MSG_RESPONSE_ID_OFFSET,
   id_msg_calibration_read_done_ret_t = 0x1053 + MSG_RESPONSE_ID_OFFSET,
 
 
   /* 1808 for disp control */
   id_msg_disp_set_frame_output_t = 0x1070,
   id_msg_disp_set_frame_format_t,
   id_msg_disp_set_frame_type_t,
   id_msg_disp_set_pro_time_t,
   id_msg_disp_set_pro_current_t,
   id_msg_disp_set_denoise_t,
   id_msg_disp_set_led_on_off_t,
 
   /* 0xf000 ~ 0xfdff id reversed. */
   id_msg_xfile_import_t = 0xfe00,
   id_msg_xfile_export_t,
   id_msg_xfile_mkdir_t
 
};
 
 
#define PREISP_CALIB_ITEM_NUM       24
#define PREISP_CALIB_MAGIC      "#SLM_CALIB_DATA#"
 
struct calib_item {
   unsigned char name[48];
   unsigned int  offset;
   unsigned int  size;
   unsigned int  temp;
   unsigned int  crc32;
};
 
struct calib_head {
   unsigned char magic[16];
   unsigned int  version;
   unsigned int  head_size;
   unsigned int  image_size;
   unsigned int  items_number;
   unsigned char reserved0[32];
   unsigned int  hash_len;
   unsigned char hash[32];
   unsigned char reserved1[28];
   unsigned int  sign_tag;
   unsigned int  sign_len;
   unsigned char rsa_hash[256];
   unsigned char reserved2[120];
   struct calib_item item[PREISP_CALIB_ITEM_NUM];
};
 
#define XFILE_MAX_PATH 256
struct msg_xfile {
   u32 size;
   u16 type;
   s8  camera_id;
   union {
       s8 sync;
       s8 ret;
   };
   u32 addr;
   u32 data_size;
   u32 cb;
   u32 args;
   char path[XFILE_MAX_PATH];
};
 
int rk1608_send_msg_to_dsp(struct rk1608_state *pdata, struct msg *m);
/**
 * rk1608_write - RK1608 synchronous write
 *
 * @spi: spi device
 * @addr: resource address
 * @data: data buffer
 * @data_len: data buffer size, in bytes
 * Context: can sleep
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_write(struct spi_device *spi, s32 addr,
        const s32 *data, size_t data_len);
 
/**
 * rk1608_safe_write - RK1608 synchronous write with state check
 *
 * @spi: spi device
 * @addr: resource address
 * @data: data buffer
 * @data_len: data buffer size, in bytes
 * Context: can sleep
 *
 * It returns zero on success, else operation state code.
 */
int rk1608_safe_write(struct rk1608_state *rk1608, struct spi_device *spi,
             s32 addr, const s32 *data, size_t data_len);
 
/**
 * rk1608_read - RK1608 synchronous read
 *
 * @spi: spi device
 * @addr: resource address
 * @data: data buffer [out]
 * @data_len: data buffer size, in bytes
 * Context: can sleep
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_read(struct spi_device *spi, s32 addr,
       s32 *data, size_t data_len);
 
/**
 * rk1608_safe_read - RK1608 synchronous read with state check
 *
 * @spi: spi device
 * @addr: resource address
 * @data: data buffer [out]
 * @data_len: data buffer size, in bytes
 * Context: can sleep
 *
 * It returns zero on success, else operation state code.
 */
int rk1608_safe_read(struct rk1608_state *rk1608, struct spi_device *spi,
            s32 addr, s32 *data, size_t data_len);
 
/**
 * rk1608_operation_query - RK1608 last operation state query
 *
 * @spi: spi device
 * @state: last operation state [out]
 * Context: can sleep
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_operation_query(struct spi_device *spi, s32 *state);
 
/**
 * rk1608_state_query - RK1608 system state query
 *
 * @spi: spi device
 * @state: system state [out]
 * Context: can sleep
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_state_query(struct spi_device *spi, int32_t *state);
 
/**
 * rk1608_interrupt_request - RK1608 request a rk1608 interrupt
 *
 * @spi: spi device
 * @interrupt_num: interrupt identification
 * Context: can sleep
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_interrupt_request(struct spi_device *spi, s32 interrupt_num);
 
/**
 * rk1608_download_fw: - rk1608 firmware download through spi
 *
 * @spi: spi device
 * @fw_name: name of firmware file, NULL for default firmware name
 * Context: can sleep
 *
 * It returns zero on success, else a negative error code.
 **/
int rk1608_download_fw(struct rk1608_state *rk1608, struct spi_device *spi,
           const char *fw_name);
 
/**
 * rk1608_msq_recv_msg - receive a msg from RK1608 -> AP msg queue
 *
 * @spi: spi device
 * @m: a msg pointer buf [out]
 *
 * need call rk1608_msq_free_received_msg to free msg after msg use done
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_msq_recv_msg(struct rk1608_state *rk1608, struct spi_device *spi,
           struct msg **m);
 
/*
 * rk1608_msq_send_msg - send a msg from AP -> RK1608 msg queue
 *
 * @spi: spi device
 * @m: a msg to send
 *
 * It returns zero on success, else a negative error code.
 */
int rk1608_msq_send_msg(struct rk1608_state *rk1608, struct spi_device *spi,
           struct msg *m);
 
int rk1608_set_power(struct rk1608_state *pdata, int on);
 
void rk1608_set_spi_speed(struct rk1608_state *pdata, u32 hz);
 
int rk1608_set_log_level(struct rk1608_state *pdata, int level);
 
#endif