1/* SPDX-License-Identifier: GPL-2.0-or-later */ 2/* 3 * Header for M-5MOLS 8M Pixel camera sensor with ISP 4 * 5 * Copyright (C) 2011 Samsung Electronics Co., Ltd. 6 * Author: HeungJun Kim <riverful.kim@samsung.com> 7 * 8 * Copyright (C) 2009 Samsung Electronics Co., Ltd. 9 * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com> 10 */ 11 12#ifndef M5MOLS_H 13#define M5MOLS_H 14 15#include <linux/sizes.h> 16#include <media/v4l2-subdev.h> 17#include "m5mols_reg.h" 18 19 20/* An amount of data transmitted in addition to the value 21 * determined by CAPP_JPEG_SIZE_MAX register. 22 */ 23#define M5MOLS_JPEG_TAGS_SIZE 0x20000 24#define M5MOLS_MAIN_JPEG_SIZE_MAX (5 * SZ_1M) 25 26extern int m5mols_debug; 27 28enum m5mols_restype { 29 M5MOLS_RESTYPE_MONITOR, 30 M5MOLS_RESTYPE_CAPTURE, 31 M5MOLS_RESTYPE_MAX, 32}; 33 34/** 35 * struct m5mols_resolution - structure for the resolution 36 * @type: resolution type according to the pixel code 37 * @width: width of the resolution 38 * @height: height of the resolution 39 * @reg: resolution preset register value 40 */ 41struct m5mols_resolution { 42 u8 reg; 43 enum m5mols_restype type; 44 u16 width; 45 u16 height; 46}; 47 48/** 49 * struct m5mols_exif - structure for the EXIF information of M-5MOLS 50 * @exposure_time: exposure time register value 51 * @shutter_speed: speed of the shutter register value 52 * @aperture: aperture register value 53 * @brightness: brightness register value 54 * @exposure_bias: it calls also EV bias 55 * @iso_speed: ISO register value 56 * @flash: status register value of the flash 57 * @sdr: status register value of the Subject Distance Range 58 * @qval: not written exact meaning in document 59 */ 60struct m5mols_exif { 61 u32 exposure_time; 62 u32 shutter_speed; 63 u32 aperture; 64 u32 brightness; 65 u32 exposure_bias; 66 u16 iso_speed; 67 u16 flash; 68 u16 sdr; 69 u16 qval; 70}; 71 72/** 73 * struct m5mols_capture - Structure for the capture capability 74 * @exif: EXIF information 75 * @buf_size: internal JPEG frame buffer size, in bytes 76 * @main: size in bytes of the main image 77 * @thumb: size in bytes of the thumb image, if it was accompanied 78 * @total: total size in bytes of the produced image 79 */ 80struct m5mols_capture { 81 struct m5mols_exif exif; 82 unsigned int buf_size; 83 u32 main; 84 u32 thumb; 85 u32 total; 86}; 87 88/** 89 * struct m5mols_scenemode - structure for the scenemode capability 90 * @metering: metering light register value 91 * @ev_bias: EV bias register value 92 * @wb_mode: mode which means the WhiteBalance is Auto or Manual 93 * @wb_preset: whitebalance preset register value in the Manual mode 94 * @chroma_en: register value whether the Chroma capability is enabled or not 95 * @chroma_lvl: chroma's level register value 96 * @edge_en: register value Whether the Edge capability is enabled or not 97 * @edge_lvl: edge's level register value 98 * @af_range: Auto Focus's range 99 * @fd_mode: Face Detection mode 100 * @mcc: Multi-axis Color Conversion which means emotion color 101 * @light: status of the Light 102 * @flash: status of the Flash 103 * @tone: Tone color which means Contrast 104 * @iso: ISO register value 105 * @capt_mode: Mode of the Image Stabilization while the camera capturing 106 * @wdr: Wide Dynamic Range register value 107 * 108 * The each value according to each scenemode is recommended in the documents. 109 */ 110struct m5mols_scenemode { 111 u8 metering; 112 u8 ev_bias; 113 u8 wb_mode; 114 u8 wb_preset; 115 u8 chroma_en; 116 u8 chroma_lvl; 117 u8 edge_en; 118 u8 edge_lvl; 119 u8 af_range; 120 u8 fd_mode; 121 u8 mcc; 122 u8 light; 123 u8 flash; 124 u8 tone; 125 u8 iso; 126 u8 capt_mode; 127 u8 wdr; 128}; 129 130#define VERSION_STRING_SIZE 22 131 132/** 133 * struct m5mols_version - firmware version information 134 * @customer: customer information 135 * @project: version of project information according to customer 136 * @fw: firmware revision 137 * @hw: hardware revision 138 * @param: version of the parameter 139 * @awb: Auto WhiteBalance algorithm version 140 * @str: information about manufacturer and packaging vendor 141 * @af: Auto Focus version 142 * 143 * The register offset starts the customer version at 0x0, and it ends 144 * the awb version at 0x09. The customer, project information occupies 1 bytes 145 * each. And also the fw, hw, param, awb each requires 2 bytes. The str is 146 * unique string associated with firmware's version. It includes information 147 * about manufacturer and the vendor of the sensor's packaging. The least 148 * significant 2 bytes of the string indicate packaging manufacturer. 149 */ 150struct m5mols_version { 151 u8 customer; 152 u8 project; 153 u16 fw; 154 u16 hw; 155 u16 param; 156 u16 awb; 157 u8 str[VERSION_STRING_SIZE]; 158 u8 af; 159}; 160 161/** 162 * struct m5mols_info - M-5MOLS driver data structure 163 * @pdata: platform data 164 * @sd: v4l-subdev instance 165 * @pad: media pad 166 * @irq_waitq: waitqueue for the capture 167 * @irq_done: set to 1 in the interrupt handler 168 * @handle: control handler 169 * @auto_exposure: auto/manual exposure control 170 * @exposure_bias: exposure compensation control 171 * @exposure: manual exposure control 172 * @metering: exposure metering control 173 * @auto_iso: auto/manual ISO sensitivity control 174 * @iso: manual ISO sensitivity control 175 * @auto_wb: auto white balance control 176 * @lock_3a: 3A lock control 177 * @colorfx: color effect control 178 * @saturation: saturation control 179 * @zoom: zoom control 180 * @wdr: wide dynamic range control 181 * @stabilization: image stabilization control 182 * @jpeg_quality: JPEG compression quality control 183 * @set_power: optional power callback to the board code 184 * @lock: mutex protecting the structure fields below 185 * @ffmt: current fmt according to resolution type 186 * @res_type: current resolution type 187 * @ver: information of the version 188 * @cap: the capture mode attributes 189 * @isp_ready: 1 when the ISP controller has completed booting 190 * @power: current sensor's power status 191 * @ctrl_sync: 1 when the control handler state is restored in H/W 192 * @resolution: register value for current resolution 193 * @mode: register value for current operation mode 194 */ 195struct m5mols_info { 196 const struct m5mols_platform_data *pdata; 197 struct v4l2_subdev sd; 198 struct media_pad pad; 199 200 wait_queue_head_t irq_waitq; 201 atomic_t irq_done; 202 203 struct v4l2_ctrl_handler handle; 204 struct { 205 /* exposure/exposure bias/auto exposure cluster */ 206 struct v4l2_ctrl *auto_exposure; 207 struct v4l2_ctrl *exposure_bias; 208 struct v4l2_ctrl *exposure; 209 struct v4l2_ctrl *metering; 210 }; 211 struct { 212 /* iso/auto iso cluster */ 213 struct v4l2_ctrl *auto_iso; 214 struct v4l2_ctrl *iso; 215 }; 216 struct v4l2_ctrl *auto_wb; 217 218 struct v4l2_ctrl *lock_3a; 219 struct v4l2_ctrl *colorfx; 220 struct v4l2_ctrl *saturation; 221 struct v4l2_ctrl *zoom; 222 struct v4l2_ctrl *wdr; 223 struct v4l2_ctrl *stabilization; 224 struct v4l2_ctrl *jpeg_quality; 225 226 int (*set_power)(struct device *dev, int on); 227 228 struct mutex lock; 229 230 struct v4l2_mbus_framefmt ffmt[M5MOLS_RESTYPE_MAX]; 231 int res_type; 232 233 struct m5mols_version ver; 234 struct m5mols_capture cap; 235 236 unsigned int isp_ready:1; 237 unsigned int power:1; 238 unsigned int ctrl_sync:1; 239 240 u8 resolution; 241 u8 mode; 242}; 243 244#define is_available_af(__info) (__info->ver.af) 245#define is_code(__code, __type) (__code == m5mols_default_ffmt[__type].code) 246#define is_manufacturer(__info, __manufacturer) \ 247 (__info->ver.str[0] == __manufacturer[0] && \ 248 __info->ver.str[1] == __manufacturer[1]) 249/* 250 * I2C operation of the M-5MOLS 251 * 252 * The I2C read operation of the M-5MOLS requires 2 messages. The first 253 * message sends the information about the command, command category, and total 254 * message size. The second message is used to retrieve the data specified in 255 * the first message 256 * 257 * 1st message 2nd message 258 * +-------+---+----------+-----+-------+ +------+------+------+------+ 259 * | size1 | R | category | cmd | size2 | | d[0] | d[1] | d[2] | d[3] | 260 * +-------+---+----------+-----+-------+ +------+------+------+------+ 261 * - size1: message data size(5 in this case) 262 * - size2: desired buffer size of the 2nd message 263 * - d[0..3]: according to size2 264 * 265 * The I2C write operation needs just one message. The message includes 266 * category, command, total size, and desired data. 267 * 268 * 1st message 269 * +-------+---+----------+-----+------+------+------+------+ 270 * | size1 | W | category | cmd | d[0] | d[1] | d[2] | d[3] | 271 * +-------+---+----------+-----+------+------+------+------+ 272 * - d[0..3]: according to size1 273 */ 274int m5mols_read_u8(struct v4l2_subdev *sd, u32 reg_comb, u8 *val); 275int m5mols_read_u16(struct v4l2_subdev *sd, u32 reg_comb, u16 *val); 276int m5mols_read_u32(struct v4l2_subdev *sd, u32 reg_comb, u32 *val); 277int m5mols_write(struct v4l2_subdev *sd, u32 reg_comb, u32 val); 278 279int m5mols_busy_wait(struct v4l2_subdev *sd, u32 reg, u32 value, u32 mask, 280 int timeout); 281 282/* Mask value for busy waiting until M-5MOLS I2C interface is initialized */ 283#define M5MOLS_I2C_RDY_WAIT_FL (1 << 16) 284/* ISP state transition timeout, in ms */ 285#define M5MOLS_MODE_CHANGE_TIMEOUT 200 286#define M5MOLS_BUSY_WAIT_DEF_TIMEOUT 250 287 288/* 289 * Mode operation of the M-5MOLS 290 * 291 * Changing the mode of the M-5MOLS is needed right executing order. 292 * There are three modes(PARAMETER, MONITOR, CAPTURE) which can be changed 293 * by user. There are various categories associated with each mode. 294 * 295 * +============================================================+ 296 * | mode | category | 297 * +============================================================+ 298 * | FLASH | FLASH(only after Stand-by or Power-on) | 299 * | SYSTEM | SYSTEM(only after sensor arm-booting) | 300 * | PARAMETER | PARAMETER | 301 * | MONITOR | MONITOR(preview), Auto Focus, Face Detection | 302 * | CAPTURE | Single CAPTURE, Preview(recording) | 303 * +============================================================+ 304 * 305 * The available executing order between each modes are as follows: 306 * PARAMETER <---> MONITOR <---> CAPTURE 307 */ 308int m5mols_set_mode(struct m5mols_info *info, u8 mode); 309 310int m5mols_enable_interrupt(struct v4l2_subdev *sd, u8 reg); 311int m5mols_wait_interrupt(struct v4l2_subdev *sd, u8 condition, u32 timeout); 312int m5mols_restore_controls(struct m5mols_info *info); 313int m5mols_start_capture(struct m5mols_info *info); 314int m5mols_do_scenemode(struct m5mols_info *info, u8 mode); 315int m5mols_lock_3a(struct m5mols_info *info, bool lock); 316int m5mols_set_ctrl(struct v4l2_ctrl *ctrl); 317int m5mols_init_controls(struct v4l2_subdev *sd); 318 319/* The firmware function */ 320int m5mols_update_fw(struct v4l2_subdev *sd, 321 int (*set_power)(struct m5mols_info *, bool)); 322 323static inline struct m5mols_info *to_m5mols(struct v4l2_subdev *subdev) 324{ 325 return container_of(subdev, struct m5mols_info, sd); 326} 327 328static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl) 329{ 330 struct m5mols_info *info = container_of(ctrl->handler, 331 struct m5mols_info, handle); 332 return &info->sd; 333} 334 335static inline void m5mols_set_ctrl_mode(struct v4l2_ctrl *ctrl, 336 unsigned int mode) 337{ 338 ctrl->priv = (void *)(uintptr_t)mode; 339} 340 341static inline unsigned int m5mols_get_ctrl_mode(struct v4l2_ctrl *ctrl) 342{ 343 return (unsigned int)(uintptr_t)ctrl->priv; 344} 345 346#endif /* M5MOLS_H */ 347