Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c7dca221 authored by Kyle Yan's avatar Kyle Yan Committed by Gerrit - the friendly Code Review server
Browse files

Merge "msm: camera: Add camera common utilities" into msm-4.9

parents fdee88f0 bbe33743
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
ccflags-y += -Idrivers/media/platform/msm/camera/cam_req_mgr

obj-$(CONFIG_SPECTRA_CAMERA) += cam_req_mgr/
obj-$(CONFIG_SPECTRA_CAMERA) += cam_utils/
+1 −0
Original line number Diff line number Diff line
obj-$(CONFIG_SPECTRA_CAMERA) += cam_soc_util.o cam_io_util.o
+284 −0
Original line number Diff line number Diff line
/* Copyright (c) 2011-2014, 2017, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__

#include <linux/delay.h>
#include <linux/io.h>
#include <linux/err.h>
#include "cam_io_util.h"

#undef CDBG
#define CDBG(fmt, args...) pr_debug(fmt, ##args)

int cam_io_w(uint32_t data, void __iomem *addr)
{
	if (!addr)
		return -EINVAL;

	CDBG("0x%pK %08x\n", addr, data);
	writel_relaxed(data, addr);

	return 0;
}

int cam_io_w_mb(uint32_t data, void __iomem *addr)
{
	if (!addr)
		return -EINVAL;

	CDBG("0x%pK %08x\n", addr, data);
	/* Ensure previous writes are done */
	wmb();
	writel_relaxed(data, addr);

	return 0;
}

uint32_t cam_io_r(void __iomem *addr)
{
	uint32_t data;

	if (!addr) {
		pr_err("Invalid args\n");
		return 0;
	}

	data = readl_relaxed(addr);
	CDBG("0x%pK %08x\n", addr, data);

	return data;
}

uint32_t cam_io_r_mb(void __iomem *addr)
{
	uint32_t data;

	if (!addr) {
		pr_err("Invalid args\n");
		return 0;
	}

	/* Ensure previous read is done */
	rmb();
	data = readl_relaxed(addr);
	CDBG("0x%pK %08x\n", addr, data);

	return data;
}

int cam_io_memcpy(void __iomem *dest_addr,
	void __iomem *src_addr, uint32_t len)
{
	int i;
	uint32_t *d = (uint32_t *) dest_addr;
	uint32_t *s = (uint32_t *) src_addr;

	if (!dest_addr || !src_addr)
		return -EINVAL;

	CDBG("%pK %pK %d\n", dest_addr, src_addr, len);

	for (i = 0; i < len/4; i++) {
		CDBG("0x%pK %08x\n", d, *s);
		writel_relaxed(*s++, d++);
	}

	return 0;
}

int  cam_io_memcpy_mb(void __iomem *dest_addr,
	void __iomem *src_addr, uint32_t len)
{
	int i;
	uint32_t *d = (uint32_t *) dest_addr;
	uint32_t *s = (uint32_t *) src_addr;

	if (!dest_addr || !src_addr)
		return -EINVAL;

	CDBG("%pK %pK %d\n", dest_addr, src_addr, len);

	/*
	 * Do not use cam_io_w_mb to avoid double wmb() after a write
	 * and before the next write.
	 */
	wmb();
	for (i = 0; i < (len / 4); i++) {
		CDBG("0x%pK %08x\n", d, *s);
		writel_relaxed(*s++, d++);
	}

	return 0;
}

int cam_io_poll_value(void __iomem *addr, uint32_t wait_data, uint32_t retry,
	unsigned long min_usecs, unsigned long max_usecs)
{
	uint32_t tmp, cnt = 0;
	int rc = 0;

	if (!addr)
		return -EINVAL;

	tmp = readl_relaxed(addr);
	while ((tmp != wait_data) && (cnt++ < retry)) {
		if (min_usecs > 0 && max_usecs > 0)
			usleep_range(min_usecs, max_usecs);
		tmp = readl_relaxed(addr);
	}

	if (cnt > retry) {
		pr_debug("Poll failed by value\n");
		rc = -EINVAL;
	}

	return rc;
}

int cam_io_poll_value_wmask(void __iomem *addr, uint32_t wait_data,
	uint32_t bmask, uint32_t retry, unsigned long min_usecs,
	unsigned long max_usecs)
{
	uint32_t tmp, cnt = 0;
	int rc = 0;

	if (!addr)
		return -EINVAL;

	tmp = readl_relaxed(addr);
	while (((tmp & bmask) != wait_data) && (cnt++ < retry)) {
		if (min_usecs > 0 && max_usecs > 0)
			usleep_range(min_usecs, max_usecs);
		tmp = readl_relaxed(addr);
	}

	if (cnt > retry) {
		pr_debug("Poll failed with mask\n");
		rc = -EINVAL;
	}

	return rc;
}

int cam_io_w_same_offset_block(const uint32_t *data, void __iomem *addr,
	uint32_t len)
{
	int i;

	if (!data || !len || !addr)
		return -EINVAL;

	for (i = 0; i < len; i++) {
		CDBG("i= %d len =%d val=%x addr =%pK\n",
			i, len, data[i], addr);
		writel_relaxed(data[i], addr);
	}

	return 0;
}

int cam_io_w_mb_same_offset_block(const uint32_t *data, void __iomem *addr,
	uint32_t len)
{
	int i;

	if (!data || !len || !addr)
		return -EINVAL;

	for (i = 0; i < len; i++) {
		CDBG("i= %d len =%d val=%x addr =%pK\n",
			i, len, data[i], addr);
		/* Ensure previous writes are done */
		wmb();
		writel_relaxed(data[i], addr);
	}

	return 0;
}

#define __OFFSET(__i)   (data[__i][0])
#define __VAL(__i)      (data[__i][1])
int cam_io_w_offset_val_block(const uint32_t data[][2],
	void __iomem *addr_base, uint32_t len)
{
	int i;

	if (!data || !len || !addr_base)
		return -EINVAL;

	for (i = 0; i < len; i++) {
		CDBG("i= %d len =%d val=%x addr_base =%pK reg=%x\n",
			i, len, __VAL(i), addr_base, __OFFSET(i));
		writel_relaxed(__VAL(i), addr_base + __OFFSET(i));
	}

	return 0;
}

int cam_io_w_mb_offset_val_block(const uint32_t data[][2],
	void __iomem *addr_base, uint32_t len)
{
	int i;

	if (!data || !len || !addr_base)
		return -EINVAL;

	/* Ensure write is done */
	wmb();
	for (i = 0; i < len; i++) {
		CDBG("i= %d len =%d val=%x addr_base =%pK reg=%x\n",
			i, len, __VAL(i), addr_base, __OFFSET(i));
		writel_relaxed(__VAL(i), addr_base + __OFFSET(i));
	}

	return 0;
}

#define BYTES_PER_REGISTER           4
#define NUM_REGISTER_PER_LINE        4
#define REG_OFFSET(__start, __i)    (__start + (__i * BYTES_PER_REGISTER))
int cam_io_dump(void __iomem *base_addr, uint32_t start_offset, int size)
{
	char          line_str[128];
	char         *p_str;
	int           i;
	uint32_t      data;

	CDBG("addr=%pK offset=0x%x size=%d\n", base_addr, start_offset, size);

	if (!base_addr || (size <= 0))
		return -EINVAL;

	line_str[0] = '\0';
	p_str = line_str;
	for (i = 0; i < size; i++) {
		if (i % NUM_REGISTER_PER_LINE == 0) {
			snprintf(p_str, 12, "0x%08x: ",
				REG_OFFSET(start_offset, i));
			p_str += 12;
		}
		data = readl_relaxed(base_addr + REG_OFFSET(start_offset, i));
		snprintf(p_str, 9, "%08x ", data);
		p_str += 9;
		if ((i + 1) % NUM_REGISTER_PER_LINE == 0) {
			pr_err("%s\n", line_str);
			line_str[0] = '\0';
			p_str = line_str;
		}
	}
	if (line_str[0] != '\0')
		pr_err("%s\n", line_str);

	return 0;
}
+239 −0
Original line number Diff line number Diff line
/* Copyright (c) 2011-2014, 2017, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifndef _CAM_IO_UTIL_H_
#define _CAM_IO_UTIL_H_

#include <linux/types.h>

/**
 * cam_io_w()
 *
 * @brief:              Camera IO util for register write
 *
 * @data:               Value to be written
 * @addr:               Address used to write the value
 *
 * @return:             Success or Failure
 */
int cam_io_w(uint32_t data, void __iomem *addr);

/**
 * cam_io_w_mb()
 *
 * @brief:              Camera IO util for register write with memory barrier.
 *                      Memory Barrier is only before the write to ensure the
 *                      order. If need to ensure this write is also flushed
 *                      call wmb() independently in the caller.
 *
 * @data:               Value to be written
 * @addr:               Address used to write the value
 *
 * @return:             Success or Failure
 */
int cam_io_w_mb(uint32_t data, void __iomem *addr);

/**
 * cam_io_r()
 *
 * @brief:              Camera IO util for register read
 *
 * @addr:               Address of register to be read
 *
 * @return:             Value read from the register address
 */
uint32_t cam_io_r(void __iomem *addr);

/**
 * cam_io_r_mb()
 *
 * @brief:              Camera IO util for register read with memory barrier.
 *                      Memory Barrier is only before the write to ensure the
 *                      order. If need to ensure this write is also flushed
 *                      call rmb() independently in the caller.
 *
 * @addr:               Address of register to be read
 *
 * @return:             Value read from the register address
 */
uint32_t cam_io_r_mb(void __iomem *addr);

/**
 * cam_io_memcpy()
 *
 * @brief:              Camera IO util for memory to register copy
 *
 * @dest_addr:          Destination register address
 * @src_addr:           Source regiser address
 * @len:                Range to be copied
 *
 * @return:             Success or Failure
 */
int cam_io_memcpy(void __iomem *dest_addr,
		void __iomem *src_addr, uint32_t len);

/**
 * cam_io_memcpy_mb()
 *
 * @brief:              Camera IO util for memory to register copy
 *                      with barrier.
 *                      Memory Barrier is only before the write to ensure the
 *                      order. If need to ensure this write is also flushed
 *                      call wmb() independently in the caller.
 *
 * @dest_addr:          Destination register address
 * @src_addr:           Source regiser address
 * @len:                Range to be copied
 *
 * @return:             Success or Failure
 */
int cam_io_memcpy_mb(void __iomem *dest_addr,
	void __iomem *src_addr, uint32_t len);

/**
 * cam_io_poll_value_wmask()
 *
 * @brief:              Poll register value with bitmask.
 *
 * @addr:               Register address to be polled
 * @wait_data:          Wait until @bmask read from @addr matches this data
 * @bmask:              Bit mask
 * @retry:              Number of retry
 * @min_usecs:          Minimum time to wait for retry
 * @max_usecs:          Maximum time to wait for retry
 *
 * @return:             Success or Failure
 *
 * This function can sleep so it should not be called from interrupt
 * handler, spin_lock etc.
 */
int cam_io_poll_value_wmask(void __iomem *addr, uint32_t wait_data,
	uint32_t bmask, uint32_t retry, unsigned long min_usecs,
	unsigned long max_usecs);

/**
 * cam_io_poll_value()
 *
 * @brief:              Poll register value
 *
 * @addr:               Register address to be polled
 * @wait_data:          Wait until value read from @addr matches this data
 * @retry:              Number of retry
 * @min_usecs:          Minimum time to wait for retry
 * @max_usecs:          Maximum time to wait for retry
 *
 * @return:             Success or Failure
 *
 * This function can sleep so it should not be called from interrupt
 * handler, spin_lock etc.
 */
int cam_io_poll_value(void __iomem *addr, uint32_t wait_data, uint32_t retry,
	unsigned long min_usecs, unsigned long max_usecs);

/**
 * cam_io_w_same_offset_block()
 *
 * @brief:              Write a block of data to same address
 *
 * @data:               Block data to be written
 * @addr:               Register offset to be written.
 * @len:                Number of the data to be written
 *
 * @return:             Success or Failure
 */
int cam_io_w_same_offset_block(const uint32_t *data, void __iomem *addr,
	uint32_t len);

/**
 * cam_io_w_mb_same_offset_block()
 *
 * @brief:              Write a block of data to same address with barrier.
 *                      Memory Barrier is only before the write to ensure the
 *                      order. If need to ensure this write is also flushed
 *                      call wmb() independently in the caller.
 *
 * @data:               Block data to be written
 * @addr:               Register offset to be written.
 * @len:                Number of the data to be written
 *
 * @return:             Success or Failure
 */
int cam_io_w_mb_same_offset_block(const uint32_t *data, void __iomem *addr,
	uint32_t len);

/**
 * cam_io_w_offset_val_block()
 *
 * @brief:              This API is to write a block of registers
 *                      represented by a 2 dimensional array table with
 *                      register offset and value pair
 *
 *  offset0, value0,
 *  offset1, value1,
 *  offset2, value2,
 *  and so on...
 *
 * @data:               Pointer to 2-dimensional offset-value array
 * @addr_base:          Base address to which offset will be added to
 *                      get the register address
 * @len:                Length of offset-value pair array to be written in
 *                      number of uin32_t
 *
 * @return:             Success or Failure
 *
 */
int32_t cam_io_w_offset_val_block(const uint32_t data[][2],
	void __iomem *addr_base, uint32_t len);

/**
 * cam_io_w_mb_offset_val_block()
 *
 * @brief:              This API is to write a block of registers
 *                      represented by a 2 dimensional array table with
 *                      register offset and value pair with memory barrier.
 *                      Memory Barrier is only before the write to ensure the
 *                      order. If need to ensure this write is also flushed
 *                      call wmb() independently in the caller.
 *                      The OFFSETS NEED to be different because of the way
 *                      barrier is used here.
 *
 *  offset0, value0,
 *  offset1, value1,
 *  offset2, value2,
 *  and so on...
 *
 * @data:               Pointer to 2-dimensional offset-value array
 * @addr_base:          Base address to which offset will be added to
 *                      get the register address
 * @len:                Length of offset-value pair array to be written in
 *                      number of uin32_t
 *
 * @return:             Success or Failure
 *
 */
int32_t cam_io_w_mb_offset_val_block(const uint32_t data[][2],
	void __iomem *addr_base, uint32_t len);

/**
 * cam_io_dump()
 *
 * @brief:              Camera IO util for dumping a range of register
 *
 * @base_addr:          Start register address for the dumping
 * @start_offset:       Start register offset for the dump
 * @size:               Size specifying the range for dumping
 *
 * @return:             Success or Failure
 */
int cam_io_dump(void __iomem *base_addr, uint32_t start_offset, int size);

#endif /* _CAM_IO_UTIL_H_ */
+598 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading