+
+int ipc_dev_raw_cmd(struct intel_ipc_dev *ipc_dev, u32 *cmd_list, u32
cmdlen,
+ u8 *in, u32 inlen, u32 *out, u32 outlen, u32 dptr, u32 sptr) {
+ int ret;
+ int inbuflen = DIV_ROUND_UP(inlen, 4);
+ u32 *inbuf;
+
+ if (!cmd_list || !in)
+ return -EINVAL;
+
+ inbuf = kzalloc(inbuflen, GFP_KERNEL);
+ if (!inbuf)
+ return -ENOMEM;
+
+ ret = ipc_dev_lock(ipc_dev);
+ if (ret)
+ return ret;
+
+ /* Call custom pre-processing handler. */
+ if (ipc_dev->ops->pre_raw_cmd_fn) {
+ ret = ipc_dev->ops->pre_raw_cmd_fn(cmd_list, cmdlen, in,
inlen,
+ out, outlen, dptr, sptr);
+ if (ret)
+ goto unlock_device;
+ }
+
+ /* If supported, write DPTR register.*/
+ if (ipc_dev->cfg->support_dptr)
+ regmap_write(ipc_dev->cfg->cmd_regs, ipc_dev->cfg-
dptr_reg,
+ dptr);
+
+ /* If supported, write SPTR register. */
+ if (ipc_dev->cfg->support_sptr)
+ regmap_write(ipc_dev->cfg->cmd_regs, ipc_dev->cfg-
sptr_reg,
+ sptr);
+
+ memcpy(inbuf, in, inlen);
+
+ /* Write inlen dwords of data to wrbuf_reg. */
+ if (inlen > 0)
+ regmap_bulk_write(ipc_dev->cfg->data_regs,
+ ipc_dev->cfg->wrbuf_reg, inbuf, inbuflen);
+
+ ipc_dev_send_cmd(ipc_dev, cmd_list[0]);
+
+ ret = ipc_dev_check_status(ipc_dev);
+
+ /* Read outlen dwords of data from rbug_reg. */
+ if (!ret && outlen > 0)
+ regmap_bulk_read(ipc_dev->cfg->data_regs,
+ ipc_dev->cfg->rbuf_reg, out, outlen);
+unlock_device:
+ ipc_dev_unlock(ipc_dev);
+ kfree(inbuf);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(ipc_dev_raw_cmd);
+
+/* sysfs option to send simple IPC commands from userspace */ static
+ssize_t ipc_dev_cmd_reg_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count) {
+ struct intel_ipc_dev *ipc_dev = dev_get_drvdata(dev);
+ u32 cmd;
+ int ret;
+
+ ret = sscanf(buf, "%d", &cmd);
+ if (ret != 1) {
+ dev_err(dev, "Error args\n");
+ return -EINVAL;
+ }
+
+ ret = ipc_dev_simple_cmd(ipc_dev, &cmd, 1);
+ if (ret) {
+ dev_err(dev, "command 0x%x error with %d\n", cmd, ret);
+ return ret;
+ }
+ return (ssize_t)count;
+}
+
+static DEVICE_ATTR(send_cmd, S_IWUSR, NULL, ipc_dev_cmd_reg_store);
+
+static struct attribute *ipc_dev_attrs[] = {
+ &dev_attr_send_cmd.attr,
+ NULL
+};
+
+static const struct attribute_group ipc_dev_group = {
+ .attrs = ipc_dev_attrs,
+};
+
+static const struct attribute_group *ipc_dev_groups[] = {
+ &ipc_dev_group,
+ NULL,
+};
+
+/* IPC device IRQ handler */
+static irqreturn_t ipc_dev_irq_handler(int irq, void *dev_id) {
+ struct intel_ipc_dev *ipc_dev = (struct intel_ipc_dev *)dev_id;
+
+ if (ipc_dev->ops->pre_irq_handler_fn)
+ ipc_dev->ops->pre_irq_handler_fn(ipc_dev, irq);
+
+ complete(&ipc_dev->cmd_complete);
+
+ return IRQ_HANDLED;
+}
+
+static void devm_intel_ipc_dev_release(struct device *dev, void *res) {
+ struct intel_ipc_dev *ipc_dev = *(struct intel_ipc_dev **)res;
+
+ if (!ipc_dev)
+ return;
+
+ device_del(&ipc_dev->dev);
+
+ kfree(ipc_dev);
+}
+
+static int match_name(struct device *dev, const void *data) {
+ if (!dev_name(dev))
+ return 0;
+
+ return !strcmp(dev_name(dev), (char *)data); }
+
+/**
+ * intel_ipc_dev_get() - Get Intel IPC device from name.
+ * @dev_name : Name of the IPC device.
+ *
+ * Return : ERR_PTR/NULL or intel_ipc_dev pointer on success.
+ */
+struct intel_ipc_dev *intel_ipc_dev_get(const char *dev_name) {
+ struct device *dev;
+
+ if (!dev_name)
+ return ERR_PTR(-EINVAL);
+
+ dev = class_find_device(&intel_ipc_class, NULL, dev_name,
match_name);
+
+ return dev ? dev_get_drvdata(dev) : NULL; }
+EXPORT_SYMBOL_GPL(intel_ipc_dev_get);
+
+static void devm_intel_ipc_dev_put(struct device *dev, void *res) {
+ intel_ipc_dev_put(*(struct intel_ipc_dev **)res); }
+
+/**
+ * devm_intel_ipc_dev_get() - Resource managed version of
intel_ipc_dev_get().
+ * @dev : Device pointer.
+ * @dev_name : Name of the IPC device.
+ *
+ * Return : ERR_PTR/NULL or intel_ipc_dev pointer on success.
+ */
+struct intel_ipc_dev *devm_intel_ipc_dev_get(struct device *dev,
+ const char *dev_name)
+{
+ struct intel_ipc_dev **ptr, *ipc_dev;
+
+ ptr = devres_alloc(devm_intel_ipc_dev_put, sizeof(*ptr),
GFP_KERNEL);
+ if (!ptr)
+ return ERR_PTR(-ENOMEM);
+
+ ipc_dev = intel_ipc_dev_get(dev_name);
+ if (!IS_ERR_OR_NULL(ipc_dev)) {
+ *ptr = ipc_dev;
+ devres_add(dev, ptr);
+ } else {
+ devres_free(ptr);
+ }
+
+ return ipc_dev;
+}
+EXPORT_SYMBOL_GPL(devm_intel_ipc_dev_get);
+
+/**
+ * devm_intel_ipc_dev_create() - Create IPC device
+ * @dev : IPC parent device.
+ * @devname : Name of the IPC device.
+ * @cfg : IPC device configuration.
+ * @ops : IPC device ops.
+ *
+ * Resource managed API to create IPC device with
+ * given configuration.
+ *
+ * Return : IPC device pointer or ERR_PTR(error code).
+ */
+struct intel_ipc_dev *devm_intel_ipc_dev_create(struct device *dev,
+ const char *devname,
+ struct intel_ipc_dev_cfg *cfg,
+ struct intel_ipc_dev_ops *ops)
+{
+ struct intel_ipc_dev **ptr, *ipc_dev;
+ int ret;
+
+ if (!dev && !devname && !cfg)
+ return ERR_PTR(-EINVAL);
+
+ if (intel_ipc_dev_get(devname)) {
+ dev_err(dev, "IPC device %s already exist\n", devname);
+ return ERR_PTR(-EINVAL);
+ }
+
+ ptr = devres_alloc(devm_intel_ipc_dev_release, sizeof(*ptr),
+ GFP_KERNEL);
+ if (!ptr)
+ return ERR_PTR(-ENOMEM);
+
+ ipc_dev = kzalloc(sizeof(*ipc_dev), GFP_KERNEL);
+ if (!ipc_dev) {
+ ret = -ENOMEM;
+ goto err_dev_create;
+ }
+
+ ipc_dev->dev.class = &intel_ipc_class;
+ ipc_dev->dev.parent = dev;
+ ipc_dev->dev.groups = ipc_dev_groups;
+ ipc_dev->cfg = cfg;
+ ipc_dev->ops = ops;
+
+ mutex_init(&ipc_dev->lock);
+ init_completion(&ipc_dev->cmd_complete);
+ dev_set_drvdata(&ipc_dev->dev, ipc_dev);
+ dev_set_name(&ipc_dev->dev, devname);
+ device_initialize(&ipc_dev->dev);
+
+ ret = device_add(&ipc_dev->dev);
+ if (ret < 0) {
+ dev_err(&ipc_dev->dev, "%s device create failed\n",
+ __func__);
+ ret = -ENODEV;
+ goto err_dev_add;
+ }
+
+ if (ipc_dev->cfg->mode == IPC_DEV_MODE_IRQ) {
+ if (devm_request_irq(&ipc_dev->dev,
+ ipc_dev->cfg->irq,
+ ipc_dev_irq_handler,
+ ipc_dev->cfg->irqflags,
+ dev_name(&ipc_dev->dev),
+ ipc_dev)) {
+ dev_err(&ipc_dev->dev,
+ "Failed to request irq\n");
+ goto err_irq_request;
+ }
+ }
+
+ *ptr = ipc_dev;
+
+ devres_add(dev, ptr);
+
+ return ipc_dev;
+
+err_irq_request:
+ device_del(&ipc_dev->dev);
+err_dev_add:
+ kfree(ipc_dev);
+err_dev_create:
+ devres_free(ptr);
+ return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(devm_intel_ipc_dev_create);
+
+static int __init intel_ipc_init(void)
+{
+ ipc_channel_lock_init();
+ return class_register(&intel_ipc_class); }
+
+static void __exit intel_ipc_exit(void) {
+ class_unregister(&intel_ipc_class);
+}
+subsys_initcall(intel_ipc_init);
+module_exit(intel_ipc_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Kuppuswamy
+Sathyanarayanan<sathyanarayanan.kuppuswamy@xxxxxxxxxxxxxxx>");
+MODULE_DESCRIPTION("Intel IPC device class driver");
diff --git a/include/linux/platform_data/x86/intel_ipc_dev.h
b/include/linux/platform_data/x86/intel_ipc_dev.h
new file mode 100644
index 0000000..eaeedaf
--- /dev/null
+++ b/include/linux/platform_data/x86/intel_ipc_dev.h
@@ -0,0 +1,206 @@
+/*
+ * Intel IPC class device header file.
+ *
+ * (C) Copyright 2017 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ *
+ */
+
+#ifndef INTEL_IPC_DEV_H
+#define INTEL_IPC_DEV_H
+
+#include <linux/module.h>
+#include <linux/device.h>
+
+/* IPC channel type */
+#define IPC_CHANNEL_IA_PMC 0
+#define IPC_CHANNEL_IA_PUNIT 1
+#define IPC_CHANNEL_PMC_PUNIT 2
+#define IPC_CHANNEL_IA_SCU 3
+#define IPC_CHANNEL_MAX 4
+
+/* IPC return code */
+#define IPC_DEV_ERR_NONE 0
+#define IPC_DEV_ERR_CMD_NOT_SUPPORTED 1
+#define IPC_DEV_ERR_CMD_NOT_SERVICED 2
+#define IPC_DEV_ERR_UNABLE_TO_SERVICE 3
+#define IPC_DEV_ERR_CMD_INVALID 4
+#define IPC_DEV_ERR_CMD_FAILED 5
+#define IPC_DEV_ERR_EMSECURITY 6
+#define IPC_DEV_ERR_UNSIGNEDKERNEL 7
+#define IPC_DEV_ERR_MAX 8
+
+/* IPC mode */
+#define IPC_DEV_MODE_IRQ 0
+#define IPC_DEV_MODE_POLLING 1
+
+/* IPC dev constants */
+#define IPC_DEV_CMD_LOOP_CNT 3000000
+#define IPC_DEV_CMD_TIMEOUT 3 * HZ
+#define IPC_DEV_DATA_BUFFER_SIZE 16
+
+struct intel_ipc_dev;
+struct intel_ipc_raw_cmd;
+
+/**
+ * struct intel_ipc_dev_cfg - IPC device config structure.
+ *
+ * IPC device drivers uses the following config options to
+ * register new IPC device.
+ *
+ * @cmd_regs : IPC device command base regmap.
+ * @data_regs : IPC device data base regmap.
+ * @wrbuf_reg : IPC device data write register address.
+ * @rbuf_reg : IPC device data read register address.
+ * @sptr_reg : IPC device source data pointer register address.
+ * @dptr_reg : IPC device destination data pointer register
+ * address.
+ * @status_reg : IPC command status register address.
+ * @cmd_reg : IPC command register address.
+ * @mode : IRQ/POLLING mode.
+ * @irq : IPC device IRQ number.
+ * @irqflags : IPC device IRQ flags.
+ * @chan_type : IPC device channel type(PMC/PUNIT).
+ * @msi : Enable/Disable MSI for IPC commands.
+ * @support_dptr : Support DPTR update.
+ * @support_sptr : Support SPTR update.
+ *
+ */
+struct intel_ipc_dev_cfg {
+ struct regmap *cmd_regs;
+ struct regmap *data_regs;
+ unsigned int wrbuf_reg;
+ unsigned int rbuf_reg;
+ unsigned int sptr_reg;
+ unsigned int dptr_reg;
+ unsigned int status_reg;
+ unsigned int cmd_reg;
+ int mode;
+ int irq;
+ int irqflags;
+ int chan_type;
+ bool use_msi;
+ bool support_dptr;
+ bool support_sptr;
+};
+
+/**
+ * struct intel_ipc_dev_ops - IPC device ops structure.
+ *
+ * Call backs for IPC device specific operations.
+ *
+ * @to_err_code : Status to error code conversion function.
+ * @busy_check : Check for IPC busy status.
+ * @enable_msi : Enable MSI for IPC commands.
+ * @pre_simple_cmd_fn : Custom pre-processing function for
+ * ipc_dev_simple_cmd()
+ * @pre_cmd_fn : Custom pre-processing function for
+ * ipc_dev_cmd()
+ * @pre_raw_cmd_fn : Custom pre-processing function for
+ * ipc_dev_raw_cmd()
+ *
+ */
+struct intel_ipc_dev_ops {
+ int (*to_err_code)(int status);
+ int (*busy_check)(int status);
+ u32 (*enable_msi)(u32 cmd);
+ int (*pre_simple_cmd_fn)(u32 *cmd_list, u32 cmdlen);
+ int (*pre_cmd_fn)(u32 *cmd_list, u32 cmdlen, u32 *in, u32 inlen,
+ u32 *out, u32 outlen);
+ int (*pre_raw_cmd_fn)(u32 *cmd_list, u32 cmdlen, u8 *in, u32 inlen,
+ u32 *out, u32 outlen, u32 dptr, u32 sptr);
+ int (*pre_irq_handler_fn)(struct intel_ipc_dev *ipc_dev, int irq); };
+
+/**
+ * struct intel_ipc_dev - Intel IPC device structure.
+ *
+ * Used with devm_intel_ipc_dev_create() to create new IPC device.
+ *
+ * @dev : IPC device object.
+ * @cmd : Current IPC device command.
+ * @cmd_complete : Command completion object.
+ * @lock : Lock to protect IPC device structure.
+ * @ops : IPC device ops pointer.
+ * @cfg : IPC device cfg pointer.
+ *
+ */
+struct intel_ipc_dev {
+ struct device dev;
+ int cmd;
+ struct completion cmd_complete;
+ struct mutex lock;
+ struct intel_ipc_dev_ops *ops;
+ struct intel_ipc_dev_cfg *cfg;
+};
+
+#if IS_ENABLED(CONFIG_INTEL_IPC_DEV)
+
+/* API to create new IPC device */
+struct intel_ipc_dev *devm_intel_ipc_dev_create(struct device *dev,
+ const char *devname, struct intel_ipc_dev_cfg *cfg,
+ struct intel_ipc_dev_ops *ops);
+
+int ipc_dev_simple_cmd(struct intel_ipc_dev *ipc_dev, u32 *cmd_list,
+ u32 cmdlen);
+int ipc_dev_cmd(struct intel_ipc_dev *ipc_dev, u32 *cmd_list, u32 cmdlen,
+ u32 *in, u32 inlen, u32 *out, u32 outlen); int
ipc_dev_raw_cmd(struct
+intel_ipc_dev *ipc_dev, u32 *cmd_list, u32 cmdlen,
+ u8 *in, u32 inlen, u32 *out, u32 outlen, u32 dptr, u32 sptr);
struct
+intel_ipc_dev *intel_ipc_dev_get(const char *dev_name); struct
+intel_ipc_dev *devm_intel_ipc_dev_get(struct device *dev,
+ const char *dev_name);
+static inline void intel_ipc_dev_put(struct intel_ipc_dev *ipc_dev) {
+ put_device(&ipc_dev->dev);
+}
+#else
+
+static inline struct intel_ipc_dev *devm_intel_ipc_dev_create(
+ struct device *dev,
+ const char *devname, struct intel_ipc_dev_cfg *cfg,
+ struct intel_ipc_dev_ops *ops)
+{
+ return -EINVAL;
+}
+
+static inline int ipc_dev_simple_cmd(struct intel_ipc_dev *ipc_dev,
+ u32 *cmd_list, u32 cmdlen)
+{
+ return -EINVAL;
+}
+
+static int ipc_dev_cmd(struct intel_ipc_dev *ipc_dev, u32 *cmd_list,
+ u32 cmdlen, u32 *in, u32 inlen, u32 *out, u32 outlen) {
+ return -EINVAL;
+}
+
+static inline int ipc_dev_raw_cmd(struct intel_ipc_dev *ipc_dev, u32
*cmd_list,
+ u32 cmdlen, u8 *in, u32 inlen, u32 *out, u32 outlen,
+ u32 dptr, u32 sptr);
+{
+ return -EINVAL;
+}
+
+static inline struct intel_ipc_dev *intel_ipc_dev_get(const char
+*dev_name) {
+ return NULL;
+}
+
+static inline struct intel_ipc_dev *devm_intel_ipc_dev_get(struct device
*dev,
+ const char *dev_name);
+{
+ return NULL;
+}
+
+static inline void intel_ipc_dev_put(struct intel_ipc_dev *ipc_dev) {
+ return NULL;
+}
+#endif /* CONFIG_INTEL_IPC_DEV */
+#endif /* INTEL_IPC_DEV_H */
--
2.7.4