microchip-isc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Microchip Image Sensor Controller (ISC) driver header file
  4. *
  5. * Copyright (C) 2016-2019 Microchip Technology, Inc.
  6. *
  7. * Author: Songjun Wu
  8. * Author: Eugen Hristev <eugen.hristev@microchip.com>
  9. *
  10. */
  11. #ifndef _MICROCHIP_ISC_H_
  12. #include <linux/clk-provider.h>
  13. #include <linux/platform_device.h>
  14. #include <media/v4l2-ctrls.h>
  15. #include <media/v4l2-device.h>
  16. #include <media/videobuf2-dma-contig.h>
  17. #define ISC_CLK_MAX_DIV 255
  18. enum isc_clk_id {
  19. ISC_ISPCK = 0,
  20. ISC_MCK = 1,
  21. };
  22. struct isc_clk {
  23. struct clk_hw hw;
  24. struct clk *clk;
  25. struct regmap *regmap;
  26. spinlock_t lock; /* serialize access to clock registers */
  27. u8 id;
  28. u8 parent_id;
  29. u32 div;
  30. struct device *dev;
  31. };
  32. #define to_isc_clk(v) container_of(v, struct isc_clk, hw)
  33. struct isc_buffer {
  34. struct vb2_v4l2_buffer vb;
  35. struct list_head list;
  36. };
  37. struct isc_subdev_entity {
  38. struct v4l2_subdev *sd;
  39. struct v4l2_async_connection *asd;
  40. struct device_node *epn;
  41. struct v4l2_async_notifier notifier;
  42. u32 pfe_cfg0;
  43. struct list_head list;
  44. };
  45. /*
  46. * struct isc_format - ISC media bus format information
  47. This structure represents the interface between the ISC
  48. and the sensor. It's the input format received by
  49. the ISC.
  50. * @fourcc: Fourcc code for this format
  51. * @mbus_code: V4L2 media bus format code.
  52. * @cfa_baycfg: If this format is RAW BAYER, indicate the type of bayer.
  53. this is either BGBG, RGRG, etc.
  54. * @pfe_cfg0_bps: Number of hardware data lines connected to the ISC
  55. * @raw: If the format is raw bayer.
  56. */
  57. struct isc_format {
  58. u32 fourcc;
  59. u32 mbus_code;
  60. u32 cfa_baycfg;
  61. u32 pfe_cfg0_bps;
  62. bool raw;
  63. };
  64. /* Pipeline bitmap */
  65. #define DPC_DPCENABLE BIT(0)
  66. #define DPC_GDCENABLE BIT(1)
  67. #define DPC_BLCENABLE BIT(2)
  68. #define WB_ENABLE BIT(3)
  69. #define CFA_ENABLE BIT(4)
  70. #define CC_ENABLE BIT(5)
  71. #define GAM_ENABLE BIT(6)
  72. #define GAM_BENABLE BIT(7)
  73. #define GAM_GENABLE BIT(8)
  74. #define GAM_RENABLE BIT(9)
  75. #define VHXS_ENABLE BIT(10)
  76. #define CSC_ENABLE BIT(11)
  77. #define CBC_ENABLE BIT(12)
  78. #define SUB422_ENABLE BIT(13)
  79. #define SUB420_ENABLE BIT(14)
  80. #define GAM_ENABLES (GAM_RENABLE | GAM_GENABLE | GAM_BENABLE | GAM_ENABLE)
  81. /*
  82. * struct fmt_config - ISC format configuration and internal pipeline
  83. This structure represents the internal configuration
  84. of the ISC.
  85. It also holds the format that ISC will present to v4l2.
  86. * @sd_format: Pointer to an isc_format struct that holds the sensor
  87. configuration.
  88. * @fourcc: Fourcc code for this format.
  89. * @bpp: Bytes per pixel in the current format.
  90. * @bpp_v4l2: Bytes per pixel in the current format, for v4l2.
  91. This differs from 'bpp' in the sense that in planar
  92. formats, it refers only to the first plane.
  93. * @rlp_cfg_mode: Configuration of the RLP (rounding, limiting packaging)
  94. * @dcfg_imode: Configuration of the input of the DMA module
  95. * @dctrl_dview: Configuration of the output of the DMA module
  96. * @bits_pipeline: Configuration of the pipeline, which modules are enabled
  97. */
  98. struct fmt_config {
  99. struct isc_format *sd_format;
  100. u32 fourcc;
  101. u8 bpp;
  102. u8 bpp_v4l2;
  103. u32 rlp_cfg_mode;
  104. u32 dcfg_imode;
  105. u32 dctrl_dview;
  106. u32 bits_pipeline;
  107. };
  108. #define HIST_ENTRIES 512
  109. #define HIST_BAYER (ISC_HIS_CFG_MODE_B + 1)
  110. enum{
  111. HIST_INIT = 0,
  112. HIST_ENABLED,
  113. HIST_DISABLED,
  114. };
  115. struct isc_ctrls {
  116. struct v4l2_ctrl_handler handler;
  117. u32 brightness;
  118. u32 contrast;
  119. u8 gamma_index;
  120. #define ISC_WB_NONE 0
  121. #define ISC_WB_AUTO 1
  122. #define ISC_WB_ONETIME 2
  123. u8 awb;
  124. /* one for each component : GR, R, GB, B */
  125. u32 gain[HIST_BAYER];
  126. s32 offset[HIST_BAYER];
  127. u32 hist_entry[HIST_ENTRIES];
  128. u32 hist_count[HIST_BAYER];
  129. u8 hist_id;
  130. u8 hist_stat;
  131. #define HIST_MIN_INDEX 0
  132. #define HIST_MAX_INDEX 1
  133. u32 hist_minmax[HIST_BAYER][2];
  134. };
  135. #define ISC_PIPE_LINE_NODE_NUM 15
  136. /*
  137. * struct isc_reg_offsets - ISC device register offsets
  138. * @csc: Offset for the CSC register
  139. * @cbc: Offset for the CBC register
  140. * @sub422: Offset for the SUB422 register
  141. * @sub420: Offset for the SUB420 register
  142. * @rlp: Offset for the RLP register
  143. * @his: Offset for the HIS related registers
  144. * @dma: Offset for the DMA related registers
  145. * @version: Offset for the version register
  146. * @his_entry: Offset for the HIS entries registers
  147. */
  148. struct isc_reg_offsets {
  149. u32 csc;
  150. u32 cbc;
  151. u32 sub422;
  152. u32 sub420;
  153. u32 rlp;
  154. u32 his;
  155. u32 dma;
  156. u32 version;
  157. u32 his_entry;
  158. };
  159. enum isc_mc_pads {
  160. ISC_PAD_SINK = 0,
  161. ISC_PADS_NUM = 1,
  162. };
  163. enum isc_scaler_pads {
  164. ISC_SCALER_PAD_SINK = 0,
  165. ISC_SCALER_PAD_SOURCE = 1,
  166. ISC_SCALER_PADS_NUM = 2,
  167. };
  168. /*
  169. * struct isc_device - ISC device driver data/config struct
  170. * @regmap: Register map
  171. * @hclock: Hclock clock input (refer datasheet)
  172. * @ispck: iscpck clock (refer datasheet)
  173. * @isc_clks: ISC clocks
  174. * @ispck_required: ISC requires ISP Clock initialization
  175. * @dcfg: DMA master configuration, architecture dependent
  176. *
  177. * @dev: Registered device driver
  178. * @v4l2_dev: v4l2 registered device
  179. * @video_dev: registered video device
  180. *
  181. * @vb2_vidq: video buffer 2 video queue
  182. * @dma_queue_lock: lock to serialize the dma buffer queue
  183. * @dma_queue: the queue for dma buffers
  184. * @cur_frm: current isc frame/buffer
  185. * @sequence: current frame number
  186. * @stop: true if isc is not streaming, false if streaming
  187. * @comp: completion reference that signals frame completion
  188. *
  189. * @fmt: current v42l format
  190. * @try_fmt: current v4l2 try format
  191. *
  192. * @config: current ISC format configuration
  193. * @try_config: the current ISC try format , not yet activated
  194. *
  195. * @ctrls: holds information about ISC controls
  196. * @do_wb_ctrl: control regarding the DO_WHITE_BALANCE button
  197. * @awb_work: workqueue reference for autowhitebalance histogram
  198. * analysis
  199. *
  200. * @lock: lock for serializing userspace file operations
  201. * with ISC operations
  202. * @awb_mutex: serialize access to streaming status from awb work queue
  203. * @awb_lock: lock for serializing awb work queue operations
  204. * with DMA/buffer operations
  205. *
  206. * @pipeline: configuration of the ISC pipeline
  207. *
  208. * @current_subdev: current subdevice: the sensor
  209. * @subdev_entities: list of subdevice entitites
  210. *
  211. * @gamma_table: pointer to the table with gamma values, has
  212. * gamma_max sets of GAMMA_ENTRIES entries each
  213. * @gamma_max: maximum number of sets of inside the gamma_table
  214. *
  215. * @max_width: maximum frame width, dependent on the internal RAM
  216. * @max_height: maximum frame height, dependent on the internal RAM
  217. *
  218. * @config_dpc: pointer to a function that initializes product
  219. * specific DPC module
  220. * @config_csc: pointer to a function that initializes product
  221. * specific CSC module
  222. * @config_cbc: pointer to a function that initializes product
  223. * specific CBC module
  224. * @config_cc: pointer to a function that initializes product
  225. * specific CC module
  226. * @config_gam: pointer to a function that initializes product
  227. * specific GAMMA module
  228. * @config_rlp: pointer to a function that initializes product
  229. * specific RLP module
  230. * @config_ctrls: pointer to a functoin that initializes product
  231. * specific v4l2 controls.
  232. *
  233. * @adapt_pipeline: pointer to a function that adapts the pipeline bits
  234. * to the product specific pipeline
  235. *
  236. * @offsets: struct holding the product specific register offsets
  237. * @controller_formats: pointer to the array of possible formats that the
  238. * controller can output
  239. * @formats_list: pointer to the array of possible formats that can
  240. * be used as an input to the controller
  241. * @controller_formats_size: size of controller_formats array
  242. * @formats_list_size: size of formats_list array
  243. * @pads: media controller pads for isc video entity
  244. * @mdev: media device that is registered by the isc
  245. * @mpipe: media device pipeline used by the isc
  246. * @remote_pad: remote pad on the connected subdevice
  247. * @scaler_sd: subdevice for the scaler that isc registers
  248. * @scaler_pads: media controller pads for the scaler subdevice
  249. * @scaler_format: current format for the scaler subdevice
  250. */
  251. struct isc_device {
  252. struct regmap *regmap;
  253. struct clk *hclock;
  254. struct clk *ispck;
  255. struct isc_clk isc_clks[2];
  256. bool ispck_required;
  257. u32 dcfg;
  258. struct device *dev;
  259. struct v4l2_device v4l2_dev;
  260. struct video_device video_dev;
  261. struct vb2_queue vb2_vidq;
  262. spinlock_t dma_queue_lock;
  263. struct list_head dma_queue;
  264. struct isc_buffer *cur_frm;
  265. unsigned int sequence;
  266. bool stop;
  267. struct completion comp;
  268. struct v4l2_format fmt;
  269. struct v4l2_format try_fmt;
  270. struct fmt_config config;
  271. struct fmt_config try_config;
  272. struct isc_ctrls ctrls;
  273. struct work_struct awb_work;
  274. struct mutex lock;
  275. struct mutex awb_mutex;
  276. spinlock_t awb_lock;
  277. struct regmap_field *pipeline[ISC_PIPE_LINE_NODE_NUM];
  278. struct isc_subdev_entity *current_subdev;
  279. struct list_head subdev_entities;
  280. struct {
  281. #define ISC_CTRL_DO_WB 1
  282. #define ISC_CTRL_R_GAIN 2
  283. #define ISC_CTRL_B_GAIN 3
  284. #define ISC_CTRL_GR_GAIN 4
  285. #define ISC_CTRL_GB_GAIN 5
  286. #define ISC_CTRL_R_OFF 6
  287. #define ISC_CTRL_B_OFF 7
  288. #define ISC_CTRL_GR_OFF 8
  289. #define ISC_CTRL_GB_OFF 9
  290. struct v4l2_ctrl *awb_ctrl;
  291. struct v4l2_ctrl *do_wb_ctrl;
  292. struct v4l2_ctrl *r_gain_ctrl;
  293. struct v4l2_ctrl *b_gain_ctrl;
  294. struct v4l2_ctrl *gr_gain_ctrl;
  295. struct v4l2_ctrl *gb_gain_ctrl;
  296. struct v4l2_ctrl *r_off_ctrl;
  297. struct v4l2_ctrl *b_off_ctrl;
  298. struct v4l2_ctrl *gr_off_ctrl;
  299. struct v4l2_ctrl *gb_off_ctrl;
  300. };
  301. #define GAMMA_ENTRIES 64
  302. /* pointer to the defined gamma table */
  303. const u32 (*gamma_table)[GAMMA_ENTRIES];
  304. u32 gamma_max;
  305. u32 max_width;
  306. u32 max_height;
  307. struct {
  308. void (*config_dpc)(struct isc_device *isc);
  309. void (*config_csc)(struct isc_device *isc);
  310. void (*config_cbc)(struct isc_device *isc);
  311. void (*config_cc)(struct isc_device *isc);
  312. void (*config_gam)(struct isc_device *isc);
  313. void (*config_rlp)(struct isc_device *isc);
  314. void (*config_ctrls)(struct isc_device *isc,
  315. const struct v4l2_ctrl_ops *ops);
  316. void (*adapt_pipeline)(struct isc_device *isc);
  317. };
  318. struct isc_reg_offsets offsets;
  319. const struct isc_format *controller_formats;
  320. struct isc_format *formats_list;
  321. u32 controller_formats_size;
  322. u32 formats_list_size;
  323. struct {
  324. struct media_pad pads[ISC_PADS_NUM];
  325. struct media_device mdev;
  326. struct media_pipeline mpipe;
  327. u32 remote_pad;
  328. };
  329. struct {
  330. struct v4l2_subdev scaler_sd;
  331. struct media_pad scaler_pads[ISC_SCALER_PADS_NUM];
  332. struct v4l2_mbus_framefmt scaler_format[ISC_SCALER_PADS_NUM];
  333. };
  334. };
  335. extern const struct regmap_config microchip_isc_regmap_config;
  336. extern const struct v4l2_async_notifier_operations microchip_isc_async_ops;
  337. irqreturn_t microchip_isc_interrupt(int irq, void *dev_id);
  338. int microchip_isc_pipeline_init(struct isc_device *isc);
  339. int microchip_isc_clk_init(struct isc_device *isc);
  340. void microchip_isc_subdev_cleanup(struct isc_device *isc);
  341. void microchip_isc_clk_cleanup(struct isc_device *isc);
  342. int isc_scaler_link(struct isc_device *isc);
  343. int isc_scaler_init(struct isc_device *isc);
  344. int isc_mc_init(struct isc_device *isc, u32 ver);
  345. void isc_mc_cleanup(struct isc_device *isc);
  346. struct isc_format *isc_find_format_by_code(struct isc_device *isc,
  347. unsigned int code, int *index);
  348. #endif