#include <linux/acpi.h>
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/time64.h>
#include <linux/workqueue.h>
#include <media/ipu-bridge.h>
#include <media/ipu6-pci-table.h>
#include "icvs.h"
#define CMD_TIMEOUT (5 * HZ)
#define FW_READY_DELAY_MS 100
#define PCI_DEVICE_ID_INTEL_IPU7 0x645d
#define PCI_DEVICE_ID_INTEL_IPU7P5 0xb05d
static const struct pci_device_id icvs_ipu7_tbl[] = {
{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IPU7) },
{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IPU7P5) },
{ }
};
static const struct acpi_gpio_params gpio_wake = { 0, 0, false };
static const struct acpi_gpio_params gpio_rst = { 1, 0, false };
static const struct acpi_gpio_params gpio_req = { 2, 0, false };
static const struct acpi_gpio_params gpio_resp = { 3, 0, false };
static const struct acpi_gpio_mapping icvs_acpi_gpios[] = {
{ "wake-gpio", &gpio_wake, 1 },
{ "rst-gpio", &gpio_rst, 1 },
{ "req-gpio", &gpio_req, 1 },
{ "resp-gpio", &gpio_resp, 1 },
{ }
};
static const struct acpi_gpio_params lgpio_req = { 0, 0, false };
static const struct acpi_gpio_params lgpio_resp = { 1, 0, false };
static const struct acpi_gpio_mapping icvs_acpi_lgpios[] = {
{ "req-gpio", &lgpio_req, 1 },
{ "resp-gpio", &lgpio_resp, 1 },
{ }
};
static const struct icvs_device_quirk cvs_quirk_table[] = {
{ 0x2ac1, 0x20d0, ICVS_NO_MIPI_CONFIG |
ICVS_NO_CAPS |
ICVS_NO_FW_UPDATE
},
{ 0x06CB, 0x0701, ICVS_SKIP_FW_RESET |
ICVS_HOST_SENSOR_PWR_CTRL |
ICVS_HOST_PRIV_CTRL |
ICVS_FW_BUF_SIZE_256 |
ICVS_FW_HEADER_SIZE_256
},
{ }
};
static void cvs_set_quirks(struct icvs *ctx, u16 vid, u16 pid)
{
ctx->quirks = 0;
for (unsigned int i = 0; i < ARRAY_SIZE(cvs_quirk_table); i++) {
if (cvs_quirk_table[i].vid == vid &&
cvs_quirk_table[i].pid == pid) {
ctx->quirks = cvs_quirk_table[i].quirks;
dev_info(cvs_dev(ctx),
"Quirks: 0x%lx (VID:0x%04x PID:0x%04x)\n",
ctx->quirks, vid, pid);
return;
}
}
dev_info(cvs_dev(ctx),
"No quirks for device (VID:0x%04x PID:0x%04x)\n", vid, pid);
}
static int cvs_read_i2c(struct icvs *ctx, __be16 cmd_id, void *resp,
size_t size)
{
size_t prefix_size = ctx->prefix ? sizeof(u32) : 0;
size_t read_size = size + prefix_size;
struct i2c_client *i2c = ctx->i2c_client;
u8 *buf __free(kfree) = NULL;
int cnt;
if (!resp || !size)
return -EINVAL;
cnt = i2c_master_send(i2c, (const char *)&cmd_id, sizeof(cmd_id));
if (cnt != sizeof(cmd_id))
return cnt < 0 ? cnt : -EIO;
buf = kmalloc(read_size, GFP_KERNEL);
if (!buf)
return -ENOMEM;
cnt = i2c_master_recv(i2c, buf, read_size);
if (cnt != read_size) {
dev_dbg(cvs_dev(ctx), "recv cmd 0x%04x short read (%d/%zu)\n",
be16_to_cpu(cmd_id), cnt, read_size);
return cnt < 0 ? cnt : -EIO;
}
memcpy(resp, buf + prefix_size, size);
return 0;
}
static int cvs_write_i2c(struct icvs *ctx, const void *data, int size)
{
struct i2c_client *i2c = ctx->i2c_client;
int cnt;
if (size < 0 || !data)
return -EINVAL;
cnt = i2c_master_send(i2c, data, size);
if (cnt != size) {
dev_dbg(cvs_dev(ctx), "send short (%d/%d)\n", cnt, size);
return cnt < 0 ? cnt : -EIO;
}
return 0;
}
static u32 cvs_checksum(const void *data, size_t len)
{
const u32 *words = data;
u32 csum = 0;
if (WARN_ON_ONCE(len % sizeof(u32)))
return 0;
for (unsigned int i = 0; i < len / sizeof(u32); i++)
csum += words[i];
return csum;
}
static int cvs_schedule_and_wait(struct icvs *ctx, unsigned int work_delay_ms,
unsigned long wait_jiffies)
{
int ret;
schedule_delayed_work(&ctx->work, msecs_to_jiffies(work_delay_ms));
ret = wait_for_completion_killable_timeout(&ctx->cmd_completion,
wait_jiffies);
if (ret < 0)
return ret;
if (!ret)
return -ETIMEDOUT;
return 0;
}
static int cvs_wait_wake_or_sleep(struct icvs *ctx,
unsigned long timeout_jiffies,
unsigned int sleep_ms)
{
int ret;
if (ctx->res == ICVS_FULLCAP) {
ret = wait_event_interruptible_timeout(ctx->hostwake_event,
ctx->hostwake_event_arg,
timeout_jiffies);
ctx->hostwake_event_arg = false;
if (ret < 0)
return ret;
if (!ret)
return -ETIMEDOUT;
return 0;
}
msleep(sleep_ms);
return 0;
}
static int cvs_config_mipi(struct icvs *ctx, struct icvs_cmd *c, size_t len)
{
struct icvs_mipi_data_packet pkt = {
.cmd_id = c->cmd_id,
.size = sizeof(c->param.conf),
.crc = cvs_checksum(&c->param.conf, sizeof(c->param.conf)),
.conf = c->param.conf,
};
if (ctx->quirks & ICVS_NO_MIPI_CONFIG)
return 0;
return cvs_write_i2c(ctx, &pkt, sizeof(pkt));
}
static int cvs_get_device_state(struct icvs *ctx, u8 *state)
{
struct icvs_resp n = {
.cmd_id = cpu_to_be16(ICVS_GET_DEV_STATE),
};
int ret;
ret = cvs_read_i2c(ctx, n.cmd_id, &n.resp.state, sizeof(n.resp.state));
if (ret)
return ret;
*state = n.resp.state;
return 0;
}
static int cvs_get_device_caps(struct icvs *ctx,
struct icvs_dev_capabilities *caps)
{
struct icvs_resp n = {
.cmd_id = cpu_to_be16(ICVS_GET_DEV_CAPABILITY),
};
int ret;
if (ctx->quirks & ICVS_NO_CAPS)
return 0;
ret = cvs_read_i2c(ctx, n.cmd_id, &n.resp.cap, sizeof(n.resp.cap));
if (ret)
return ret;
*caps = n.resp.cap;
return 0;
}
static int cvs_hw_init(struct icvs *ctx)
{
struct icvs_resp n = { };
__be16 cmd = cpu_to_be16(ICVS_GET_DEV_VID_PID);
u32 resp;
int ret;
ctx->prefix = false;
ret = cvs_read_i2c(ctx, cmd, &resp, sizeof(resp));
if (ret)
return ret;
ctx->prefix = resp == ICVS_PREFIX_VAL;
ret = cvs_read_i2c(ctx, cmd,
&n.resp.vid_pid, sizeof(n.resp.vid_pid));
if (ret)
return ret;
cvs_set_quirks(ctx, n.resp.vid_pid.v_id, n.resp.vid_pid.p_id);
return 0;
}
static irqreturn_t cvs_irq_handler(int irq, void *dev_id)
{
struct icvs *ctx = dev_id;
ctx->hostwake_event_arg = true;
wake_up_interruptible(&ctx->hostwake_event);
return IRQ_HANDLED;
}
static void cvs_reset(struct icvs *ctx)
{
if (ctx->quirks & ICVS_SKIP_FW_RESET)
return;
if (ctx->res == ICVS_FULLCAP) {
gpiod_set_value(ctx->rst, 0);
fsleep(2000);
gpiod_set_value(ctx->rst, 1);
}
}
static void cvs_recv(struct work_struct *work)
{
struct icvs *ctx = container_of(work, struct icvs, work.work);
u8 state = 0;
int ret;
ret = cvs_get_device_state(ctx, &state);
if (ret < 0) {
dev_dbg(cvs_dev(ctx), "state read failed: %d\n", ret);
return;
}
if (state & ICVS_DEV_STATE_BUSY) {
dev_dbg(cvs_dev(ctx), "device busy, reschedule\n");
schedule_delayed_work(&ctx->work,
msecs_to_jiffies(FW_READY_DELAY_MS));
return;
}
ctx->wq_resp.resp.state = state;
complete(&ctx->cmd_completion);
}
int cvs_send(struct icvs *ctx, struct icvs_cmd *cmd, size_t len)
{
int ret, status = 0;
lockdep_assert_held(&ctx->lock);
dev_dbg(cvs_dev(ctx), "send cmd = 0x%04x", be16_to_cpu(cmd->cmd_id));
reinit_completion(&ctx->cmd_completion);
switch (be16_to_cpu(cmd->cmd_id)) {
case ICVS_SET_DEV_HOST_ID:
cmd->cmd_id = cpu_to_be16(ICVS_SET_DEV_HOST_ID);
ret = cvs_write_i2c(ctx, cmd, len);
if (ret < 0)
break;
ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS,
CMD_TIMEOUT);
if (ret < 0)
break;
status = ctx->wq_resp.resp.state &
ICVS_DEV_STATE_ERROR ? -EINVAL : 0;
break;
case ICVS_HOST_SENSOR_OWNER:
gpiod_set_value_cansleep(ctx->req, cmd->param.param);
fsleep(FW_READY_DELAY_MS * USEC_PER_MSEC);
ret = gpiod_get_value_cansleep(ctx->resp);
status = cmd->param.param == ret ? 0 : -EINVAL;
ret = 0;
break;
case ICVS_HOST_SET_MIPI_CONFIG:
cmd->cmd_id = cpu_to_be16(ICVS_HOST_SET_MIPI_CONFIG);
ret = cvs_config_mipi(ctx, cmd, len);
if (ret < 0)
break;
ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS,
CMD_TIMEOUT);
status = (ctx->wq_resp.resp.state &
ICVS_DEV_STATE_ERROR) ? -EINVAL : 0;
break;
case ICVS_FW_LOADER_START:
cmd->cmd_id = cpu_to_be16(ICVS_FW_LOADER_START);
ret = cvs_write_i2c(ctx, cmd, len);
if (ret < 0)
break;
ret = cvs_wait_wake_or_sleep(ctx, CMD_TIMEOUT,
FW_READY_DELAY_MS);
if (ret)
break;
ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS,
CMD_TIMEOUT);
status = (ctx->wq_resp.resp.state &
ICVS_DEV_STATE_DOWNLOAD) ? 0 : -EINVAL;
break;
case ICVS_FW_LOADER_DATA:
if (ctx->caps.protocol_version_major >= 2 &&
ctx->caps.protocol_version_minor >= 2) {
cmd->cmd_id = cpu_to_be16(ICVS_FW_LOADER_DATA);
ret = cvs_write_i2c(ctx, cmd, len);
} else {
ret = cvs_write_i2c(ctx, &cmd->param,
len - sizeof(cmd->cmd_id));
}
if (ret < 0)
return ret;
ret = cvs_wait_wake_or_sleep(ctx, FW_READY_DELAY_MS,
FW_READY_DELAY_MS);
if (ret)
break;
ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS,
CMD_TIMEOUT);
status = ctx->wq_resp.resp.state &
ICVS_DEV_STATE_ERROR ? -EINVAL : 0;
break;
case ICVS_FW_LOADER_END:
cmd->cmd_id = cpu_to_be16(ICVS_FW_LOADER_END);
ret = cvs_write_i2c(ctx, cmd, len);
if (ret < 0)
break;
ret = cvs_wait_wake_or_sleep(ctx, CMD_TIMEOUT,
FW_READY_DELAY_MS);
if (ret)
break;
ret = cvs_schedule_and_wait(ctx, FW_READY_DELAY_MS,
CMD_TIMEOUT);
status = !(ctx->wq_resp.resp.state &
ICVS_DEV_STATE_DOWNLOAD) ? 0 : -EINVAL;
break;
default:
ret = -EINVAL;
break;
}
if (ret < 0)
return ret;
return ctx->wq_resp.status = status;
}
int cvs_set_link_owner(struct icvs *ctx, enum icvs_csi_link_owner owner)
{
struct icvs_cmd cmd = {
.cmd_id = cpu_to_be16(ICVS_HOST_SENSOR_OWNER),
.param.param = owner,
};
size_t cmd_size = sizeof(cmd.cmd_id) + sizeof(cmd.param.param);
guard(mutex)(&ctx->lock);
return cvs_send(ctx, &cmd, cmd_size);
}
static int cvs_configure_dev_caps(struct icvs *ctx)
{
struct icvs_cmd cmd = { .cmd_id = cpu_to_be16(ICVS_SET_DEV_HOST_ID) };
size_t sz = sizeof(cmd.cmd_id) + sizeof(cmd.param.host_id);
if (ctx->quirks & ICVS_NO_CAPS)
return 0;
if (ctx->quirks & ICVS_HOST_VISION_SENSING)
cmd.param.host_id |= ICVS_HOST_ID_VISION_SENSING;
if (ctx->quirks & ICVS_HOST_PRIV_CTRL)
cmd.param.host_id |= ICVS_HOST_ID_PRIVACY_LED;
if (ctx->quirks & ICVS_HOST_SENSOR_PWR_CTRL)
cmd.param.host_id |= ICVS_HOST_ID_RGBCAMERA_PWRUP;
guard(mutex)(&ctx->lock);
return cvs_send(ctx, &cmd, sz);
}
static int cvs_core_probe(struct device *dev, struct i2c_client *i2c)
{
struct pci_dev *ipu = NULL;
struct icvs *ctx;
int ret;
for (unsigned int i = 0; !ipu && ipu6_pci_tbl[i].vendor; i++)
ipu = pci_get_device(ipu6_pci_tbl[i].vendor,
ipu6_pci_tbl[i].device, NULL);
for (unsigned int i = 0; !ipu && icvs_ipu7_tbl[i].vendor; i++)
ipu = pci_get_device(icvs_ipu7_tbl[i].vendor,
icvs_ipu7_tbl[i].device, NULL);
if (!ipu)
return -ENODEV;
ret = ipu_bridge_init(&ipu->dev, ipu_bridge_parse_ssdb);
if (ret < 0)
goto err_put_ipu;
if (!dev_fwnode(dev)) {
ret = -ENXIO;
goto err_put_ipu;
}
ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
if (!ctx) {
ret = -ENOMEM;
goto err_put_ipu;
}
ctx->i2c_client = i2c;
ret = gpiod_count(dev, NULL);
switch (ret) {
case ICVS_GPIO_SYNC:
ctx->res = ICVS_LIGHTCAP;
break;
case ICVS_GPIO_ASYNC:
ctx->res = ICVS_FULLCAP;
break;
default:
dev_err(dev, "unexpected GPIO count %d\n", ret);
ret = -EINVAL;
goto err_put_ipu;
}
ret = devm_acpi_dev_add_driver_gpios(dev,
ctx->res == ICVS_FULLCAP ?
icvs_acpi_gpios :
icvs_acpi_lgpios);
if (ret) {
dev_err_probe(dev, ret, "failed to add ACPI GPIOs\n");
goto err_put_ipu;
}
ctx->req = devm_gpiod_get(dev, "req", GPIOD_OUT_HIGH);
if (IS_ERR(ctx->req)) {
ret = dev_err_probe(dev, PTR_ERR(ctx->req),
"failed to get req GPIO\n");
goto err_put_ipu;
}
ctx->resp = devm_gpiod_get(dev, "resp", GPIOD_IN);
if (IS_ERR(ctx->resp)) {
ret = dev_err_probe(dev, PTR_ERR(ctx->resp),
"failed to get resp GPIO\n");
goto err_put_ipu;
}
if (ctx->res == ICVS_FULLCAP) {
struct gpio_desc *wake;
ctx->rst = devm_gpiod_get(dev, "rst", GPIOD_OUT_HIGH);
if (IS_ERR(ctx->rst)) {
ret = dev_err_probe(dev, PTR_ERR(ctx->rst),
"failed to get rst GPIO\n");
goto err_put_ipu;
}
wake = devm_gpiod_get(dev, "wake", GPIOD_IN);
if (IS_ERR(wake)) {
ret = dev_err_probe(dev, PTR_ERR(wake),
"failed to get wake GPIO\n");
goto err_put_ipu;
}
ctx->irq = gpiod_to_irq(wake);
if (ctx->irq < 0) {
ret = dev_err_probe(dev, ctx->irq,
"failed to get wake IRQ\n");
goto err_put_ipu;
}
ret = devm_request_threaded_irq(dev, ctx->irq, NULL,
cvs_irq_handler,
IRQF_ONESHOT | IRQF_NO_SUSPEND,
"cvs_wake", ctx);
if (ret) {
dev_err_probe(dev, ret, "failed to request IRQ\n");
goto err_put_ipu;
}
}
ret = devm_mutex_init(dev, &ctx->lock);
if (ret)
goto err_put_ipu;
init_completion(&ctx->cmd_completion);
init_waitqueue_head(&ctx->hostwake_event);
INIT_DELAYED_WORK(&ctx->work, cvs_recv);
if (i2c) {
ret = cvs_hw_init(ctx);
if (ret) {
dev_err(dev, "HW init failed (%d)\n", ret);
ctx->i2c_client = NULL;
goto fail_i2c;
}
ret = cvs_get_device_caps(ctx, &ctx->caps);
if (ret) {
dev_err_probe(dev, ret, "get caps failed\n");
goto err_put_ipu;
}
ret = cvs_configure_dev_caps(ctx);
if (ret) {
dev_err_probe(dev, ret,
"configure dev caps failed\n");
goto err_put_ipu;
}
}
fail_i2c:
ret = cvs_csi_init(ctx, dev, i2c);
if (ret) {
dev_err_probe(dev, ret, "CSI init failed\n");
goto err_put_ipu;
}
dev_set_drvdata(dev, ctx);
pm_runtime_set_autosuspend_delay(dev, 1000);
pm_runtime_use_autosuspend(dev);
pm_runtime_enable(dev);
pm_runtime_idle(dev);
ctx->ipu_link = device_link_add(&ipu->dev, dev,
DL_FLAG_PM_RUNTIME |
DL_FLAG_RPM_ACTIVE |
DL_FLAG_STATELESS);
if (!ctx->ipu_link) {
dev_err(dev, "IPU device link failed\n");
ret = -ENODEV;
goto err_csi_remove;
}
if (has_acpi_companion(dev))
acpi_dev_clear_dependencies(ACPI_COMPANION(dev));
put_device(&ipu->dev);
return 0;
err_csi_remove:
if (ctx->ipu_link)
device_link_del(ctx->ipu_link);
cvs_csi_remove(ctx);
pm_runtime_dont_use_autosuspend(dev);
pm_runtime_disable(dev);
pm_runtime_set_suspended(dev);
err_put_ipu:
put_device(&ipu->dev);
return ret;
}
static int cvs_probe(struct i2c_client *i2c)
{
return cvs_core_probe(&i2c->dev, i2c);
}
static void cvs_core_remove(struct device *dev)
{
struct icvs *ctx = dev_get_drvdata(dev);
cancel_delayed_work_sync(&ctx->work);
cvs_csi_remove(ctx);
if (ctx->ipu_link)
device_link_del(ctx->ipu_link);
pm_runtime_put_noidle(dev);
pm_runtime_disable(dev);
pm_runtime_set_suspended(dev);
cvs_reset(ctx);
}
static void cvs_remove(struct i2c_client *client)
{
cvs_core_remove(&client->dev);
}
static int __maybe_unused cvs_suspend(struct device *dev)
{
struct icvs *ctx = dev_get_drvdata(dev);
cancel_delayed_work_sync(&ctx->work);
return 0;
}
static int __maybe_unused cvs_resume(struct device *dev)
{
struct icvs *ctx = dev_get_drvdata(dev);
int ret;
if (ctx->i2c_client) {
ret = cvs_hw_init(ctx);
if (ret)
return ret;
return cvs_configure_dev_caps(ctx);
}
return 0;
}
static int __maybe_unused cvs_runtime_resume(struct device *dev)
{
struct icvs *ctx = dev_get_drvdata(dev);
return cvs_set_link_owner(ctx, ICVS_CSI_LINK_HOST);
}
static int __maybe_unused cvs_runtime_suspend(struct device *dev)
{
struct icvs *ctx = dev_get_drvdata(dev);
return cvs_set_link_owner(ctx, ICVS_CSI_LINK_CVS);
}
static const struct dev_pm_ops __maybe_unused cvs_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(cvs_suspend, cvs_resume)
SET_RUNTIME_PM_OPS(cvs_runtime_suspend, cvs_runtime_resume, NULL)
};
static const struct acpi_device_id intel_cvs_acpi_match[] = {
{ "INTC10DE" },
{ "INTC10E0" },
{ "INTC10E1" },
{ }
};
MODULE_DEVICE_TABLE(acpi, intel_cvs_acpi_match);
static struct i2c_driver cvs_driver = {
.driver = {
.name = "intel_cvs",
.acpi_match_table = intel_cvs_acpi_match,
.pm = pm_ptr(&cvs_pm_ops),
},
.probe = cvs_probe,
.remove = cvs_remove,
};
static int cvs_platform_probe(struct platform_device *pdev)
{
return cvs_core_probe(&pdev->dev, NULL);
}
static void cvs_platform_remove(struct platform_device *pdev)
{
cvs_core_remove(&pdev->dev);
}
static struct platform_driver cvs_platform_driver = {
.driver = {
.name = "cvs_platform",
.acpi_match_table = intel_cvs_acpi_match,
.pm = pm_ptr(&cvs_pm_ops),
},
.probe = cvs_platform_probe,
.remove = cvs_platform_remove,
};
static int __init cvs_init(void)
{
int ret;
ret = i2c_add_driver(&cvs_driver);
if (ret)
return ret;
ret = platform_driver_register(&cvs_platform_driver);
if (ret) {
i2c_del_driver(&cvs_driver);
return ret;
}
return 0;
}
static void __exit cvs_exit(void)
{
platform_driver_unregister(&cvs_platform_driver);
i2c_del_driver(&cvs_driver);
}
module_init(cvs_init);
module_exit(cvs_exit);
MODULE_IMPORT_NS("INTEL_IPU_BRIDGE");
MODULE_AUTHOR("Miguel Vadillo <miguel.vadillo@intel.com>");
MODULE_DESCRIPTION("Intel Vision Sensing Controller driver");
MODULE_LICENSE("GPL");