18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/*
48c2ecf20Sopenharmony_ci * The Capture code for Fujitsu M-5MOLS ISP
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Samsung Electronics Co., Ltd.
78c2ecf20Sopenharmony_ci * Author: HeungJun Kim <riverful.kim@samsung.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2009 Samsung Electronics Co., Ltd.
108c2ecf20Sopenharmony_ci * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/irq.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/delay.h>
188c2ecf20Sopenharmony_ci#include <linux/gpio.h>
198c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
208c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
218c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
228c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
248c2ecf20Sopenharmony_ci#include <media/i2c/m5mols.h>
258c2ecf20Sopenharmony_ci#include <media/drv-intf/exynos-fimc.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "m5mols.h"
288c2ecf20Sopenharmony_ci#include "m5mols_reg.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci * m5mols_read_rational - I2C read of a rational number
328c2ecf20Sopenharmony_ci * @sd: sub-device, as pointed by struct v4l2_subdev
338c2ecf20Sopenharmony_ci * @addr_num: numerator register
348c2ecf20Sopenharmony_ci * @addr_den: denominator register
358c2ecf20Sopenharmony_ci * @val: place to store the division result
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * Read numerator and denominator from registers @addr_num and @addr_den
388c2ecf20Sopenharmony_ci * respectively and return the division result in @val.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistatic int m5mols_read_rational(struct v4l2_subdev *sd, u32 addr_num,
418c2ecf20Sopenharmony_ci				u32 addr_den, u32 *val)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	u32 num, den;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	int ret = m5mols_read_u32(sd, addr_num, &num);
468c2ecf20Sopenharmony_ci	if (!ret)
478c2ecf20Sopenharmony_ci		ret = m5mols_read_u32(sd, addr_den, &den);
488c2ecf20Sopenharmony_ci	if (ret)
498c2ecf20Sopenharmony_ci		return ret;
508c2ecf20Sopenharmony_ci	*val = den == 0 ? 0 : num / den;
518c2ecf20Sopenharmony_ci	return ret;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/**
558c2ecf20Sopenharmony_ci * m5mols_capture_info - Gather captured image information
568c2ecf20Sopenharmony_ci * @info: M-5MOLS driver data structure
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * For now it gathers only EXIF information and file size.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_cistatic int m5mols_capture_info(struct m5mols_info *info)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct m5mols_exif *exif = &info->cap.exif;
638c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &info->sd;
648c2ecf20Sopenharmony_ci	int ret;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	ret = m5mols_read_rational(sd, EXIF_INFO_EXPTIME_NU,
678c2ecf20Sopenharmony_ci				   EXIF_INFO_EXPTIME_DE, &exif->exposure_time);
688c2ecf20Sopenharmony_ci	if (ret)
698c2ecf20Sopenharmony_ci		return ret;
708c2ecf20Sopenharmony_ci	ret = m5mols_read_rational(sd, EXIF_INFO_TV_NU, EXIF_INFO_TV_DE,
718c2ecf20Sopenharmony_ci				   &exif->shutter_speed);
728c2ecf20Sopenharmony_ci	if (ret)
738c2ecf20Sopenharmony_ci		return ret;
748c2ecf20Sopenharmony_ci	ret = m5mols_read_rational(sd, EXIF_INFO_AV_NU, EXIF_INFO_AV_DE,
758c2ecf20Sopenharmony_ci				   &exif->aperture);
768c2ecf20Sopenharmony_ci	if (ret)
778c2ecf20Sopenharmony_ci		return ret;
788c2ecf20Sopenharmony_ci	ret = m5mols_read_rational(sd, EXIF_INFO_BV_NU, EXIF_INFO_BV_DE,
798c2ecf20Sopenharmony_ci				   &exif->brightness);
808c2ecf20Sopenharmony_ci	if (ret)
818c2ecf20Sopenharmony_ci		return ret;
828c2ecf20Sopenharmony_ci	ret = m5mols_read_rational(sd, EXIF_INFO_EBV_NU, EXIF_INFO_EBV_DE,
838c2ecf20Sopenharmony_ci				   &exif->exposure_bias);
848c2ecf20Sopenharmony_ci	if (ret)
858c2ecf20Sopenharmony_ci		return ret;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	ret = m5mols_read_u16(sd, EXIF_INFO_ISO, &exif->iso_speed);
888c2ecf20Sopenharmony_ci	if (!ret)
898c2ecf20Sopenharmony_ci		ret = m5mols_read_u16(sd, EXIF_INFO_FLASH, &exif->flash);
908c2ecf20Sopenharmony_ci	if (!ret)
918c2ecf20Sopenharmony_ci		ret = m5mols_read_u16(sd, EXIF_INFO_SDR, &exif->sdr);
928c2ecf20Sopenharmony_ci	if (!ret)
938c2ecf20Sopenharmony_ci		ret = m5mols_read_u16(sd, EXIF_INFO_QVAL, &exif->qval);
948c2ecf20Sopenharmony_ci	if (ret)
958c2ecf20Sopenharmony_ci		return ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (!ret)
988c2ecf20Sopenharmony_ci		ret = m5mols_read_u32(sd, CAPC_IMAGE_SIZE, &info->cap.main);
998c2ecf20Sopenharmony_ci	if (!ret)
1008c2ecf20Sopenharmony_ci		ret = m5mols_read_u32(sd, CAPC_THUMB_SIZE, &info->cap.thumb);
1018c2ecf20Sopenharmony_ci	if (!ret)
1028c2ecf20Sopenharmony_ci		info->cap.total = info->cap.main + info->cap.thumb;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return ret;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciint m5mols_start_capture(struct m5mols_info *info)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	unsigned int framesize = info->cap.buf_size - M5MOLS_JPEG_TAGS_SIZE;
1108c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &info->sd;
1118c2ecf20Sopenharmony_ci	int ret;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/*
1148c2ecf20Sopenharmony_ci	 * Synchronize the controls, set the capture frame resolution and color
1158c2ecf20Sopenharmony_ci	 * format. The frame capture is initiated during switching from Monitor
1168c2ecf20Sopenharmony_ci	 * to Capture mode.
1178c2ecf20Sopenharmony_ci	 */
1188c2ecf20Sopenharmony_ci	ret = m5mols_set_mode(info, REG_MONITOR);
1198c2ecf20Sopenharmony_ci	if (!ret)
1208c2ecf20Sopenharmony_ci		ret = m5mols_restore_controls(info);
1218c2ecf20Sopenharmony_ci	if (!ret)
1228c2ecf20Sopenharmony_ci		ret = m5mols_write(sd, CAPP_YUVOUT_MAIN, REG_JPEG);
1238c2ecf20Sopenharmony_ci	if (!ret)
1248c2ecf20Sopenharmony_ci		ret = m5mols_write(sd, CAPP_MAIN_IMAGE_SIZE, info->resolution);
1258c2ecf20Sopenharmony_ci	if (!ret)
1268c2ecf20Sopenharmony_ci		ret = m5mols_write(sd, CAPP_JPEG_SIZE_MAX, framesize);
1278c2ecf20Sopenharmony_ci	if (!ret)
1288c2ecf20Sopenharmony_ci		ret = m5mols_set_mode(info, REG_CAPTURE);
1298c2ecf20Sopenharmony_ci	if (!ret)
1308c2ecf20Sopenharmony_ci		/* Wait until a frame is captured to ISP internal memory */
1318c2ecf20Sopenharmony_ci		ret = m5mols_wait_interrupt(sd, REG_INT_CAPTURE, 2000);
1328c2ecf20Sopenharmony_ci	if (ret)
1338c2ecf20Sopenharmony_ci		return ret;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/*
1368c2ecf20Sopenharmony_ci	 * Initiate the captured data transfer to a MIPI-CSI receiver.
1378c2ecf20Sopenharmony_ci	 */
1388c2ecf20Sopenharmony_ci	ret = m5mols_write(sd, CAPC_SEL_FRAME, 1);
1398c2ecf20Sopenharmony_ci	if (!ret)
1408c2ecf20Sopenharmony_ci		ret = m5mols_write(sd, CAPC_START, REG_CAP_START_MAIN);
1418c2ecf20Sopenharmony_ci	if (!ret) {
1428c2ecf20Sopenharmony_ci		bool captured = false;
1438c2ecf20Sopenharmony_ci		unsigned int size;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		/* Wait for the capture completion interrupt */
1468c2ecf20Sopenharmony_ci		ret = m5mols_wait_interrupt(sd, REG_INT_CAPTURE, 2000);
1478c2ecf20Sopenharmony_ci		if (!ret) {
1488c2ecf20Sopenharmony_ci			captured = true;
1498c2ecf20Sopenharmony_ci			ret = m5mols_capture_info(info);
1508c2ecf20Sopenharmony_ci		}
1518c2ecf20Sopenharmony_ci		size = captured ? info->cap.main : 0;
1528c2ecf20Sopenharmony_ci		v4l2_dbg(1, m5mols_debug, sd, "%s: size: %d, thumb.: %d B\n",
1538c2ecf20Sopenharmony_ci			 __func__, size, info->cap.thumb);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		v4l2_subdev_notify(sd, S5P_FIMC_TX_END_NOTIFY, &size);
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return ret;
1598c2ecf20Sopenharmony_ci}
160