Files
BPI-CM9-Linux/aml-comp/hardware/amlogic/arm-isp-6.12/aml_isp5/driver/lens/lens-debugfs.c
T

136 lines
3.0 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Shared procfs focus control for aml_isp5 lens drivers.
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <media/v4l2-ctrls.h>
#include "lens-debugfs.h"
#if IS_ENABLED(CONFIG_PROC_FS)
struct aml_lens_debugfs {
struct device *dev;
struct v4l2_ctrl *focus;
u32 max_focus;
struct proc_dir_entry *lens_dir;
char dirname[48];
};
static ssize_t aml_lens_focus_read(struct file *file, char __user *ubuf,
size_t count, loff_t *ppos)
{
struct aml_lens_debugfs *dbg = pde_data(file_inode(file));
char buf[32];
int len;
if (!dbg)
return -ENODEV;
if (*ppos > 0)
return 0;
len = scnprintf(buf, sizeof(buf), "%d\n", dbg->focus->val);
return simple_read_from_buffer(ubuf, count, ppos, buf, len);
}
static ssize_t aml_lens_focus_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
struct aml_lens_debugfs *dbg = pde_data(file_inode(file));
char buf[32];
unsigned long steps;
int ret;
if (!dbg)
return -ENODEV;
if (count >= sizeof(buf))
return -EINVAL;
if (copy_from_user(buf, ubuf, count))
return -EFAULT;
buf[count] = '\0';
if (sscanf(buf, "%li", &steps) != 1)
return -EINVAL;
if (steps > dbg->max_focus)
return -EINVAL;
/*
* Keep the lens powered after proc write. pm_runtime_put() here would
* trigger autosuspend (~1s) and cut regulators via runtime_suspend.
*/
if (!pm_runtime_active(dbg->dev)) {
ret = pm_runtime_resume_and_get(dbg->dev);
if (ret < 0)
return ret;
}
ret = v4l2_ctrl_s_ctrl(dbg->focus, steps);
if (ret < 0)
return ret;
pm_runtime_mark_last_busy(dbg->dev);
return count;
}
static const struct proc_ops aml_lens_focus_proc_ops = {
.proc_read = aml_lens_focus_read,
.proc_write = aml_lens_focus_write,
.proc_lseek = default_llseek,
};
struct aml_lens_debugfs *aml_lens_debugfs_create(
const struct aml_lens_debugfs_cfg *cfg)
{
struct aml_lens_debugfs *dbg;
if (!cfg || !cfg->dev || !cfg->focus || !cfg->name_prefix)
return NULL;
dbg = devm_kzalloc(cfg->dev, sizeof(*dbg), GFP_KERNEL);
if (!dbg)
return NULL;
dbg->dev = cfg->dev;
dbg->focus = cfg->focus;
dbg->max_focus = cfg->max_focus;
snprintf(dbg->dirname, sizeof(dbg->dirname), "%s-%s", cfg->name_prefix,
dev_name(cfg->dev));
dbg->lens_dir = proc_mkdir(dbg->dirname, NULL);
if (!dbg->lens_dir) {
dev_dbg(cfg->dev, "proc mkdir /proc/%s failed\n", dbg->dirname);
return NULL;
}
if (!proc_create_data("focus", 0644, dbg->lens_dir,
&aml_lens_focus_proc_ops, dbg)) {
remove_proc_subtree(dbg->dirname, NULL);
dbg->lens_dir = NULL;
dev_dbg(cfg->dev, "proc create focus failed\n");
return NULL;
}
dev_dbg(cfg->dev, "procfs: /proc/%s/focus\n", dbg->dirname);
return dbg;
}
void aml_lens_debugfs_remove(struct aml_lens_debugfs *dbg)
{
if (!dbg || !dbg->lens_dir)
return;
remove_proc_subtree(dbg->dirname, NULL);
dbg->lens_dir = NULL;
}
#endif /* CONFIG_PROC_FS */