xref: /linux/drivers/mfd/intel-lpss.c (revision ac9538f6007e1c80f1b8a62db7ecc391b4d78ae5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24b45efe8SAndy Shevchenko /*
34b45efe8SAndy Shevchenko  * Intel Sunrisepoint LPSS core support.
44b45efe8SAndy Shevchenko  *
54b45efe8SAndy Shevchenko  * Copyright (C) 2015, Intel Corporation
64b45efe8SAndy Shevchenko  *
74b45efe8SAndy Shevchenko  * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
84b45efe8SAndy Shevchenko  *          Mika Westerberg <mika.westerberg@linux.intel.com>
94b45efe8SAndy Shevchenko  *          Heikki Krogerus <heikki.krogerus@linux.intel.com>
104b45efe8SAndy Shevchenko  *          Jarkko Nikula <jarkko.nikula@linux.intel.com>
114b45efe8SAndy Shevchenko  */
124b45efe8SAndy Shevchenko 
13a936a917SAndy Shevchenko #include <linux/array_size.h>
14a936a917SAndy Shevchenko #include <linux/bits.h>
154b45efe8SAndy Shevchenko #include <linux/clkdev.h>
16a936a917SAndy Shevchenko #include <linux/clk.h>
174b45efe8SAndy Shevchenko #include <linux/clk-provider.h>
184b45efe8SAndy Shevchenko #include <linux/debugfs.h>
19a936a917SAndy Shevchenko #include <linux/device.h>
20a936a917SAndy Shevchenko #include <linux/err.h>
21a936a917SAndy Shevchenko #include <linux/gfp_types.h>
224b45efe8SAndy Shevchenko #include <linux/idr.h>
2362e59c4eSStephen Boyd #include <linux/io.h>
244b45efe8SAndy Shevchenko #include <linux/ioport.h>
254b45efe8SAndy Shevchenko #include <linux/mfd/core.h>
26a936a917SAndy Shevchenko #include <linux/module.h>
27fd58bb8cSAndy Shevchenko #include <linux/pm.h>
284b45efe8SAndy Shevchenko #include <linux/pm_qos.h>
294b45efe8SAndy Shevchenko #include <linux/pm_runtime.h>
30a936a917SAndy Shevchenko #include <linux/sprintf.h>
31a936a917SAndy Shevchenko #include <linux/types.h>
32a936a917SAndy Shevchenko 
339cf5c095SLinus Torvalds #include <linux/io-64-nonatomic-lo-hi.h>
34689d4453SAndy Shevchenko 
35ffcfc20fSAndy Shevchenko #include <linux/dma/idma64.h>
36ffcfc20fSAndy Shevchenko 
374b45efe8SAndy Shevchenko #include "intel-lpss.h"
384b45efe8SAndy Shevchenko 
39a936a917SAndy Shevchenko struct dentry;
40a936a917SAndy Shevchenko 
414b45efe8SAndy Shevchenko #define LPSS_DEV_OFFSET		0x000
424b45efe8SAndy Shevchenko #define LPSS_DEV_SIZE		0x200
434b45efe8SAndy Shevchenko #define LPSS_PRIV_OFFSET	0x200
444b45efe8SAndy Shevchenko #define LPSS_PRIV_SIZE		0x100
4541a3da2bSHeikki Krogerus #define LPSS_PRIV_REG_COUNT	(LPSS_PRIV_SIZE / 4)
464b45efe8SAndy Shevchenko #define LPSS_IDMA64_OFFSET	0x800
474b45efe8SAndy Shevchenko #define LPSS_IDMA64_SIZE	0x800
484b45efe8SAndy Shevchenko 
494b45efe8SAndy Shevchenko /* Offsets from lpss->priv */
504b45efe8SAndy Shevchenko #define LPSS_PRIV_RESETS		0x04
51f3b23e5aSAndy Shevchenko #define LPSS_PRIV_RESETS_IDMA		BIT(2)
52f3b23e5aSAndy Shevchenko #define LPSS_PRIV_RESETS_FUNC		0x3
534b45efe8SAndy Shevchenko 
544b45efe8SAndy Shevchenko #define LPSS_PRIV_ACTIVELTR		0x10
554b45efe8SAndy Shevchenko #define LPSS_PRIV_IDLELTR		0x14
564b45efe8SAndy Shevchenko 
574b45efe8SAndy Shevchenko #define LPSS_PRIV_LTR_REQ		BIT(15)
58cbd1c5c4SAndy Shevchenko #define LPSS_PRIV_LTR_SCALE_MASK	GENMASK(11, 10)
59cbd1c5c4SAndy Shevchenko #define LPSS_PRIV_LTR_SCALE_1US		(2 << 10)
60cbd1c5c4SAndy Shevchenko #define LPSS_PRIV_LTR_SCALE_32US	(3 << 10)
61cbd1c5c4SAndy Shevchenko #define LPSS_PRIV_LTR_VALUE_MASK	GENMASK(9, 0)
624b45efe8SAndy Shevchenko 
634b45efe8SAndy Shevchenko #define LPSS_PRIV_SSP_REG		0x20
644b45efe8SAndy Shevchenko #define LPSS_PRIV_SSP_REG_DIS_DMA_FIN	BIT(0)
654b45efe8SAndy Shevchenko 
66689d4453SAndy Shevchenko #define LPSS_PRIV_REMAP_ADDR		0x40
674b45efe8SAndy Shevchenko 
684b45efe8SAndy Shevchenko #define LPSS_PRIV_CAPS			0xfc
694b45efe8SAndy Shevchenko #define LPSS_PRIV_CAPS_NO_IDMA		BIT(8)
70cbd1c5c4SAndy Shevchenko #define LPSS_PRIV_CAPS_TYPE_MASK	GENMASK(7, 4)
714b45efe8SAndy Shevchenko #define LPSS_PRIV_CAPS_TYPE_SHIFT	4
724b45efe8SAndy Shevchenko 
734b45efe8SAndy Shevchenko /* This matches the type field in CAPS register */
744b45efe8SAndy Shevchenko enum intel_lpss_dev_type {
754b45efe8SAndy Shevchenko 	LPSS_DEV_I2C = 0,
764b45efe8SAndy Shevchenko 	LPSS_DEV_UART,
774b45efe8SAndy Shevchenko 	LPSS_DEV_SPI,
784b45efe8SAndy Shevchenko };
794b45efe8SAndy Shevchenko 
804b45efe8SAndy Shevchenko struct intel_lpss {
814b45efe8SAndy Shevchenko 	const struct intel_lpss_platform_info *info;
824b45efe8SAndy Shevchenko 	enum intel_lpss_dev_type type;
834b45efe8SAndy Shevchenko 	struct clk *clk;
844b45efe8SAndy Shevchenko 	struct clk_lookup *clock;
85e15ad215SMika Westerberg 	struct mfd_cell *cell;
864b45efe8SAndy Shevchenko 	struct device *dev;
874b45efe8SAndy Shevchenko 	void __iomem *priv;
8841a3da2bSHeikki Krogerus 	u32 priv_ctx[LPSS_PRIV_REG_COUNT];
894b45efe8SAndy Shevchenko 	int devid;
904b45efe8SAndy Shevchenko 	u32 caps;
914b45efe8SAndy Shevchenko 	u32 active_ltr;
924b45efe8SAndy Shevchenko 	u32 idle_ltr;
934b45efe8SAndy Shevchenko 	struct dentry *debugfs;
944b45efe8SAndy Shevchenko };
954b45efe8SAndy Shevchenko 
964b45efe8SAndy Shevchenko static const struct resource intel_lpss_dev_resources[] = {
974b45efe8SAndy Shevchenko 	DEFINE_RES_MEM_NAMED(LPSS_DEV_OFFSET, LPSS_DEV_SIZE, "lpss_dev"),
984b45efe8SAndy Shevchenko 	DEFINE_RES_MEM_NAMED(LPSS_PRIV_OFFSET, LPSS_PRIV_SIZE, "lpss_priv"),
994b45efe8SAndy Shevchenko 	DEFINE_RES_IRQ(0),
1004b45efe8SAndy Shevchenko };
1014b45efe8SAndy Shevchenko 
1024b45efe8SAndy Shevchenko static const struct resource intel_lpss_idma64_resources[] = {
1034b45efe8SAndy Shevchenko 	DEFINE_RES_MEM(LPSS_IDMA64_OFFSET, LPSS_IDMA64_SIZE),
1044b45efe8SAndy Shevchenko 	DEFINE_RES_IRQ(0),
1054b45efe8SAndy Shevchenko };
1064b45efe8SAndy Shevchenko 
1074b45efe8SAndy Shevchenko /*
1084b45efe8SAndy Shevchenko  * Cells needs to be ordered so that the iDMA is created first. This is
1094b45efe8SAndy Shevchenko  * because we need to be sure the DMA is available when the host controller
1104b45efe8SAndy Shevchenko  * driver is probed.
1114b45efe8SAndy Shevchenko  */
1124b45efe8SAndy Shevchenko static const struct mfd_cell intel_lpss_idma64_cell = {
1134b45efe8SAndy Shevchenko 	.name = LPSS_IDMA64_DRIVER_NAME,
1144b45efe8SAndy Shevchenko 	.num_resources = ARRAY_SIZE(intel_lpss_idma64_resources),
1154b45efe8SAndy Shevchenko 	.resources = intel_lpss_idma64_resources,
1164b45efe8SAndy Shevchenko };
1174b45efe8SAndy Shevchenko 
1184b45efe8SAndy Shevchenko static const struct mfd_cell intel_lpss_i2c_cell = {
1194b45efe8SAndy Shevchenko 	.name = "i2c_designware",
1204b45efe8SAndy Shevchenko 	.num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
1214b45efe8SAndy Shevchenko 	.resources = intel_lpss_dev_resources,
1224b45efe8SAndy Shevchenko };
1234b45efe8SAndy Shevchenko 
1244b45efe8SAndy Shevchenko static const struct mfd_cell intel_lpss_uart_cell = {
1254b45efe8SAndy Shevchenko 	.name = "dw-apb-uart",
1264b45efe8SAndy Shevchenko 	.num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
1274b45efe8SAndy Shevchenko 	.resources = intel_lpss_dev_resources,
1284b45efe8SAndy Shevchenko };
1294b45efe8SAndy Shevchenko 
1304b45efe8SAndy Shevchenko static const struct mfd_cell intel_lpss_spi_cell = {
1314b45efe8SAndy Shevchenko 	.name = "pxa2xx-spi",
1324b45efe8SAndy Shevchenko 	.num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
1334b45efe8SAndy Shevchenko 	.resources = intel_lpss_dev_resources,
1344b45efe8SAndy Shevchenko };
1354b45efe8SAndy Shevchenko 
1364b45efe8SAndy Shevchenko static DEFINE_IDA(intel_lpss_devid_ida);
1374b45efe8SAndy Shevchenko static struct dentry *intel_lpss_debugfs;
1384b45efe8SAndy Shevchenko 
1394b45efe8SAndy Shevchenko static void intel_lpss_cache_ltr(struct intel_lpss *lpss)
1404b45efe8SAndy Shevchenko {
1414b45efe8SAndy Shevchenko 	lpss->active_ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
1424b45efe8SAndy Shevchenko 	lpss->idle_ltr = readl(lpss->priv + LPSS_PRIV_IDLELTR);
1434b45efe8SAndy Shevchenko }
1444b45efe8SAndy Shevchenko 
1454b45efe8SAndy Shevchenko static int intel_lpss_debugfs_add(struct intel_lpss *lpss)
1464b45efe8SAndy Shevchenko {
1474b45efe8SAndy Shevchenko 	struct dentry *dir;
1484b45efe8SAndy Shevchenko 
1494b45efe8SAndy Shevchenko 	dir = debugfs_create_dir(dev_name(lpss->dev), intel_lpss_debugfs);
1504b45efe8SAndy Shevchenko 	if (IS_ERR(dir))
1514b45efe8SAndy Shevchenko 		return PTR_ERR(dir);
1524b45efe8SAndy Shevchenko 
1534b45efe8SAndy Shevchenko 	/* Cache the values into lpss structure */
1544b45efe8SAndy Shevchenko 	intel_lpss_cache_ltr(lpss);
1554b45efe8SAndy Shevchenko 
1564b45efe8SAndy Shevchenko 	debugfs_create_x32("capabilities", S_IRUGO, dir, &lpss->caps);
1574b45efe8SAndy Shevchenko 	debugfs_create_x32("active_ltr", S_IRUGO, dir, &lpss->active_ltr);
1584b45efe8SAndy Shevchenko 	debugfs_create_x32("idle_ltr", S_IRUGO, dir, &lpss->idle_ltr);
1594b45efe8SAndy Shevchenko 
1604b45efe8SAndy Shevchenko 	lpss->debugfs = dir;
1614b45efe8SAndy Shevchenko 	return 0;
1624b45efe8SAndy Shevchenko }
1634b45efe8SAndy Shevchenko 
1644b45efe8SAndy Shevchenko static void intel_lpss_debugfs_remove(struct intel_lpss *lpss)
1654b45efe8SAndy Shevchenko {
1664b45efe8SAndy Shevchenko 	debugfs_remove_recursive(lpss->debugfs);
1674b45efe8SAndy Shevchenko }
1684b45efe8SAndy Shevchenko 
1694b45efe8SAndy Shevchenko static void intel_lpss_ltr_set(struct device *dev, s32 val)
1704b45efe8SAndy Shevchenko {
1714b45efe8SAndy Shevchenko 	struct intel_lpss *lpss = dev_get_drvdata(dev);
1724b45efe8SAndy Shevchenko 	u32 ltr;
1734b45efe8SAndy Shevchenko 
1744b45efe8SAndy Shevchenko 	/*
1754b45efe8SAndy Shevchenko 	 * Program latency tolerance (LTR) accordingly what has been asked
1764b45efe8SAndy Shevchenko 	 * by the PM QoS layer or disable it in case we were passed
1774b45efe8SAndy Shevchenko 	 * negative value or PM_QOS_LATENCY_ANY.
1784b45efe8SAndy Shevchenko 	 */
1794b45efe8SAndy Shevchenko 	ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
1804b45efe8SAndy Shevchenko 
1814b45efe8SAndy Shevchenko 	if (val == PM_QOS_LATENCY_ANY || val < 0) {
1824b45efe8SAndy Shevchenko 		ltr &= ~LPSS_PRIV_LTR_REQ;
1834b45efe8SAndy Shevchenko 	} else {
1844b45efe8SAndy Shevchenko 		ltr |= LPSS_PRIV_LTR_REQ;
1854b45efe8SAndy Shevchenko 		ltr &= ~LPSS_PRIV_LTR_SCALE_MASK;
1864b45efe8SAndy Shevchenko 		ltr &= ~LPSS_PRIV_LTR_VALUE_MASK;
1874b45efe8SAndy Shevchenko 
1884b45efe8SAndy Shevchenko 		if (val > LPSS_PRIV_LTR_VALUE_MASK)
1894b45efe8SAndy Shevchenko 			ltr |= LPSS_PRIV_LTR_SCALE_32US | val >> 5;
1904b45efe8SAndy Shevchenko 		else
1914b45efe8SAndy Shevchenko 			ltr |= LPSS_PRIV_LTR_SCALE_1US | val;
1924b45efe8SAndy Shevchenko 	}
1934b45efe8SAndy Shevchenko 
1944b45efe8SAndy Shevchenko 	if (ltr == lpss->active_ltr)
1954b45efe8SAndy Shevchenko 		return;
1964b45efe8SAndy Shevchenko 
1974b45efe8SAndy Shevchenko 	writel(ltr, lpss->priv + LPSS_PRIV_ACTIVELTR);
1984b45efe8SAndy Shevchenko 	writel(ltr, lpss->priv + LPSS_PRIV_IDLELTR);
1994b45efe8SAndy Shevchenko 
2004b45efe8SAndy Shevchenko 	/* Cache the values into lpss structure */
2014b45efe8SAndy Shevchenko 	intel_lpss_cache_ltr(lpss);
2024b45efe8SAndy Shevchenko }
2034b45efe8SAndy Shevchenko 
2044b45efe8SAndy Shevchenko static void intel_lpss_ltr_expose(struct intel_lpss *lpss)
2054b45efe8SAndy Shevchenko {
2064b45efe8SAndy Shevchenko 	lpss->dev->power.set_latency_tolerance = intel_lpss_ltr_set;
2074b45efe8SAndy Shevchenko 	dev_pm_qos_expose_latency_tolerance(lpss->dev);
2084b45efe8SAndy Shevchenko }
2094b45efe8SAndy Shevchenko 
2104b45efe8SAndy Shevchenko static void intel_lpss_ltr_hide(struct intel_lpss *lpss)
2114b45efe8SAndy Shevchenko {
2124b45efe8SAndy Shevchenko 	dev_pm_qos_hide_latency_tolerance(lpss->dev);
2134b45efe8SAndy Shevchenko 	lpss->dev->power.set_latency_tolerance = NULL;
2144b45efe8SAndy Shevchenko }
2154b45efe8SAndy Shevchenko 
2164b45efe8SAndy Shevchenko static int intel_lpss_assign_devs(struct intel_lpss *lpss)
2174b45efe8SAndy Shevchenko {
218e15ad215SMika Westerberg 	const struct mfd_cell *cell;
2194b45efe8SAndy Shevchenko 	unsigned int type;
2204b45efe8SAndy Shevchenko 
2214b45efe8SAndy Shevchenko 	type = lpss->caps & LPSS_PRIV_CAPS_TYPE_MASK;
2224b45efe8SAndy Shevchenko 	type >>= LPSS_PRIV_CAPS_TYPE_SHIFT;
2234b45efe8SAndy Shevchenko 
2244b45efe8SAndy Shevchenko 	switch (type) {
2254b45efe8SAndy Shevchenko 	case LPSS_DEV_I2C:
226e15ad215SMika Westerberg 		cell = &intel_lpss_i2c_cell;
2274b45efe8SAndy Shevchenko 		break;
2284b45efe8SAndy Shevchenko 	case LPSS_DEV_UART:
229e15ad215SMika Westerberg 		cell = &intel_lpss_uart_cell;
2304b45efe8SAndy Shevchenko 		break;
2314b45efe8SAndy Shevchenko 	case LPSS_DEV_SPI:
232e15ad215SMika Westerberg 		cell = &intel_lpss_spi_cell;
2334b45efe8SAndy Shevchenko 		break;
2344b45efe8SAndy Shevchenko 	default:
2354b45efe8SAndy Shevchenko 		return -ENODEV;
2364b45efe8SAndy Shevchenko 	}
2374b45efe8SAndy Shevchenko 
238e15ad215SMika Westerberg 	lpss->cell = devm_kmemdup(lpss->dev, cell, sizeof(*cell), GFP_KERNEL);
239e15ad215SMika Westerberg 	if (!lpss->cell)
240e15ad215SMika Westerberg 		return -ENOMEM;
241e15ad215SMika Westerberg 
2424b45efe8SAndy Shevchenko 	lpss->type = type;
2434b45efe8SAndy Shevchenko 
2444b45efe8SAndy Shevchenko 	return 0;
2454b45efe8SAndy Shevchenko }
2464b45efe8SAndy Shevchenko 
2474b45efe8SAndy Shevchenko static bool intel_lpss_has_idma(const struct intel_lpss *lpss)
2484b45efe8SAndy Shevchenko {
2494b45efe8SAndy Shevchenko 	return (lpss->caps & LPSS_PRIV_CAPS_NO_IDMA) == 0;
2504b45efe8SAndy Shevchenko }
2514b45efe8SAndy Shevchenko 
2524b45efe8SAndy Shevchenko static void intel_lpss_set_remap_addr(const struct intel_lpss *lpss)
2534b45efe8SAndy Shevchenko {
2544b45efe8SAndy Shevchenko 	resource_size_t addr = lpss->info->mem->start;
2554b45efe8SAndy Shevchenko 
256689d4453SAndy Shevchenko 	lo_hi_writeq(addr, lpss->priv + LPSS_PRIV_REMAP_ADDR);
2574b45efe8SAndy Shevchenko }
2584b45efe8SAndy Shevchenko 
2594b45efe8SAndy Shevchenko static void intel_lpss_deassert_reset(const struct intel_lpss *lpss)
2604b45efe8SAndy Shevchenko {
2614b45efe8SAndy Shevchenko 	u32 value = LPSS_PRIV_RESETS_FUNC | LPSS_PRIV_RESETS_IDMA;
2624b45efe8SAndy Shevchenko 
2634b45efe8SAndy Shevchenko 	/* Bring out the device from reset */
2644b45efe8SAndy Shevchenko 	writel(value, lpss->priv + LPSS_PRIV_RESETS);
2654b45efe8SAndy Shevchenko }
2664b45efe8SAndy Shevchenko 
2674b45efe8SAndy Shevchenko static void intel_lpss_init_dev(const struct intel_lpss *lpss)
2684b45efe8SAndy Shevchenko {
2694b45efe8SAndy Shevchenko 	u32 value = LPSS_PRIV_SSP_REG_DIS_DMA_FIN;
2704b45efe8SAndy Shevchenko 
271dad06532SBinbin Wu 	/* Set the device in reset state */
272dad06532SBinbin Wu 	writel(0, lpss->priv + LPSS_PRIV_RESETS);
273dad06532SBinbin Wu 
2744b45efe8SAndy Shevchenko 	intel_lpss_deassert_reset(lpss);
2754b45efe8SAndy Shevchenko 
276d28b6252SAndy Shevchenko 	intel_lpss_set_remap_addr(lpss);
277d28b6252SAndy Shevchenko 
2784b45efe8SAndy Shevchenko 	if (!intel_lpss_has_idma(lpss))
2794b45efe8SAndy Shevchenko 		return;
2804b45efe8SAndy Shevchenko 
2814b45efe8SAndy Shevchenko 	/* Make sure that SPI multiblock DMA transfers are re-enabled */
2824b45efe8SAndy Shevchenko 	if (lpss->type == LPSS_DEV_SPI)
2834b45efe8SAndy Shevchenko 		writel(value, lpss->priv + LPSS_PRIV_SSP_REG);
2844b45efe8SAndy Shevchenko }
2854b45efe8SAndy Shevchenko 
2864b45efe8SAndy Shevchenko static void intel_lpss_unregister_clock_tree(struct clk *clk)
2874b45efe8SAndy Shevchenko {
2884b45efe8SAndy Shevchenko 	struct clk *parent;
2894b45efe8SAndy Shevchenko 
2904b45efe8SAndy Shevchenko 	while (clk) {
2914b45efe8SAndy Shevchenko 		parent = clk_get_parent(clk);
2924b45efe8SAndy Shevchenko 		clk_unregister(clk);
2934b45efe8SAndy Shevchenko 		clk = parent;
2944b45efe8SAndy Shevchenko 	}
2954b45efe8SAndy Shevchenko }
2964b45efe8SAndy Shevchenko 
2974b45efe8SAndy Shevchenko static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
2984b45efe8SAndy Shevchenko 					     const char *devname,
2994b45efe8SAndy Shevchenko 					     struct clk **clk)
3004b45efe8SAndy Shevchenko {
3014b45efe8SAndy Shevchenko 	char name[32];
3024b45efe8SAndy Shevchenko 	struct clk *tmp = *clk;
3034b45efe8SAndy Shevchenko 
3044b45efe8SAndy Shevchenko 	snprintf(name, sizeof(name), "%s-enable", devname);
3054b45efe8SAndy Shevchenko 	tmp = clk_register_gate(NULL, name, __clk_get_name(tmp), 0,
3064b45efe8SAndy Shevchenko 				lpss->priv, 0, 0, NULL);
3074b45efe8SAndy Shevchenko 	if (IS_ERR(tmp))
3084b45efe8SAndy Shevchenko 		return PTR_ERR(tmp);
3094b45efe8SAndy Shevchenko 
3104b45efe8SAndy Shevchenko 	snprintf(name, sizeof(name), "%s-div", devname);
3114b45efe8SAndy Shevchenko 	tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
31203d790f0SAndy Shevchenko 					      0, lpss->priv, 1, 15, 16, 15,
31382f53f9eSAndy Shevchenko 					      CLK_FRAC_DIVIDER_POWER_OF_TWO_PS,
3144b45efe8SAndy Shevchenko 					      NULL);
3154b45efe8SAndy Shevchenko 	if (IS_ERR(tmp))
3164b45efe8SAndy Shevchenko 		return PTR_ERR(tmp);
3174b45efe8SAndy Shevchenko 	*clk = tmp;
3184b45efe8SAndy Shevchenko 
3194b45efe8SAndy Shevchenko 	snprintf(name, sizeof(name), "%s-update", devname);
3204b45efe8SAndy Shevchenko 	tmp = clk_register_gate(NULL, name, __clk_get_name(tmp),
3214b45efe8SAndy Shevchenko 				CLK_SET_RATE_PARENT, lpss->priv, 31, 0, NULL);
3224b45efe8SAndy Shevchenko 	if (IS_ERR(tmp))
3234b45efe8SAndy Shevchenko 		return PTR_ERR(tmp);
3244b45efe8SAndy Shevchenko 	*clk = tmp;
3254b45efe8SAndy Shevchenko 
3264b45efe8SAndy Shevchenko 	return 0;
3274b45efe8SAndy Shevchenko }
3284b45efe8SAndy Shevchenko 
3294b45efe8SAndy Shevchenko static int intel_lpss_register_clock(struct intel_lpss *lpss)
3304b45efe8SAndy Shevchenko {
3314b45efe8SAndy Shevchenko 	const struct mfd_cell *cell = lpss->cell;
3324b45efe8SAndy Shevchenko 	struct clk *clk;
3334b45efe8SAndy Shevchenko 	char devname[24];
3344b45efe8SAndy Shevchenko 	int ret;
3354b45efe8SAndy Shevchenko 
3364b45efe8SAndy Shevchenko 	if (!lpss->info->clk_rate)
3374b45efe8SAndy Shevchenko 		return 0;
3384b45efe8SAndy Shevchenko 
3394b45efe8SAndy Shevchenko 	/* Root clock */
3400f7e70e7SStephen Boyd 	clk = clk_register_fixed_rate(NULL, dev_name(lpss->dev), NULL, 0,
3410f7e70e7SStephen Boyd 				      lpss->info->clk_rate);
3424b45efe8SAndy Shevchenko 	if (IS_ERR(clk))
3434b45efe8SAndy Shevchenko 		return PTR_ERR(clk);
3444b45efe8SAndy Shevchenko 
3454b45efe8SAndy Shevchenko 	snprintf(devname, sizeof(devname), "%s.%d", cell->name, lpss->devid);
3464b45efe8SAndy Shevchenko 
3474b45efe8SAndy Shevchenko 	/*
3484b45efe8SAndy Shevchenko 	 * Support for clock divider only if it has some preset value.
3494b45efe8SAndy Shevchenko 	 * Otherwise we assume that the divider is not used.
3504b45efe8SAndy Shevchenko 	 */
3514b45efe8SAndy Shevchenko 	if (lpss->type != LPSS_DEV_I2C) {
3524b45efe8SAndy Shevchenko 		ret = intel_lpss_register_clock_divider(lpss, devname, &clk);
3534b45efe8SAndy Shevchenko 		if (ret)
3544b45efe8SAndy Shevchenko 			goto err_clk_register;
3554b45efe8SAndy Shevchenko 	}
3564b45efe8SAndy Shevchenko 
3574b45efe8SAndy Shevchenko 	ret = -ENOMEM;
3584b45efe8SAndy Shevchenko 
3594b45efe8SAndy Shevchenko 	/* Clock for the host controller */
3604b45efe8SAndy Shevchenko 	lpss->clock = clkdev_create(clk, lpss->info->clk_con_id, "%s", devname);
3614b45efe8SAndy Shevchenko 	if (!lpss->clock)
3624b45efe8SAndy Shevchenko 		goto err_clk_register;
3634b45efe8SAndy Shevchenko 
3644b45efe8SAndy Shevchenko 	lpss->clk = clk;
3654b45efe8SAndy Shevchenko 
3664b45efe8SAndy Shevchenko 	return 0;
3674b45efe8SAndy Shevchenko 
3684b45efe8SAndy Shevchenko err_clk_register:
3694b45efe8SAndy Shevchenko 	intel_lpss_unregister_clock_tree(clk);
3704b45efe8SAndy Shevchenko 
3714b45efe8SAndy Shevchenko 	return ret;
3724b45efe8SAndy Shevchenko }
3734b45efe8SAndy Shevchenko 
3744b45efe8SAndy Shevchenko static void intel_lpss_unregister_clock(struct intel_lpss *lpss)
3754b45efe8SAndy Shevchenko {
3764b45efe8SAndy Shevchenko 	if (IS_ERR_OR_NULL(lpss->clk))
3774b45efe8SAndy Shevchenko 		return;
3784b45efe8SAndy Shevchenko 
3794b45efe8SAndy Shevchenko 	clkdev_drop(lpss->clock);
3804b45efe8SAndy Shevchenko 	intel_lpss_unregister_clock_tree(lpss->clk);
3814b45efe8SAndy Shevchenko }
3824b45efe8SAndy Shevchenko 
3834b45efe8SAndy Shevchenko int intel_lpss_probe(struct device *dev,
3844b45efe8SAndy Shevchenko 		     const struct intel_lpss_platform_info *info)
3854b45efe8SAndy Shevchenko {
3864b45efe8SAndy Shevchenko 	struct intel_lpss *lpss;
3874b45efe8SAndy Shevchenko 	int ret;
3884b45efe8SAndy Shevchenko 
38992827c10SChen Ni 	if (!info || !info->mem)
3904b45efe8SAndy Shevchenko 		return -EINVAL;
3914b45efe8SAndy Shevchenko 
39292827c10SChen Ni 	if (info->irq < 0)
39392827c10SChen Ni 		return info->irq;
39492827c10SChen Ni 
3954b45efe8SAndy Shevchenko 	lpss = devm_kzalloc(dev, sizeof(*lpss), GFP_KERNEL);
3964b45efe8SAndy Shevchenko 	if (!lpss)
3974b45efe8SAndy Shevchenko 		return -ENOMEM;
3984b45efe8SAndy Shevchenko 
399a8ff78f7STuowen Zhao 	lpss->priv = devm_ioremap_uc(dev, info->mem->start + LPSS_PRIV_OFFSET,
4004b45efe8SAndy Shevchenko 				  LPSS_PRIV_SIZE);
4014b45efe8SAndy Shevchenko 	if (!lpss->priv)
4024b45efe8SAndy Shevchenko 		return -ENOMEM;
4034b45efe8SAndy Shevchenko 
4044b45efe8SAndy Shevchenko 	lpss->info = info;
4054b45efe8SAndy Shevchenko 	lpss->dev = dev;
4064b45efe8SAndy Shevchenko 	lpss->caps = readl(lpss->priv + LPSS_PRIV_CAPS);
4074b45efe8SAndy Shevchenko 
4084b45efe8SAndy Shevchenko 	dev_set_drvdata(dev, lpss);
4094b45efe8SAndy Shevchenko 
4104b45efe8SAndy Shevchenko 	ret = intel_lpss_assign_devs(lpss);
4114b45efe8SAndy Shevchenko 	if (ret)
4124b45efe8SAndy Shevchenko 		return ret;
4134b45efe8SAndy Shevchenko 
41403152e35SHeikki Krogerus 	lpss->cell->swnode = info->swnode;
415*ac9538f6SAleksandrs Vinarskis 	lpss->cell->ignore_resource_conflicts = info->quirks & QUIRK_IGNORE_RESOURCE_CONFLICTS;
416e15ad215SMika Westerberg 
4174b45efe8SAndy Shevchenko 	intel_lpss_init_dev(lpss);
4184b45efe8SAndy Shevchenko 
419895243c8SChristophe JAILLET 	lpss->devid = ida_alloc(&intel_lpss_devid_ida, GFP_KERNEL);
4204b45efe8SAndy Shevchenko 	if (lpss->devid < 0)
4214b45efe8SAndy Shevchenko 		return lpss->devid;
4224b45efe8SAndy Shevchenko 
4234b45efe8SAndy Shevchenko 	ret = intel_lpss_register_clock(lpss);
4244b45efe8SAndy Shevchenko 	if (ret)
4254b45efe8SAndy Shevchenko 		goto err_clk_register;
4264b45efe8SAndy Shevchenko 
4274b45efe8SAndy Shevchenko 	intel_lpss_ltr_expose(lpss);
4284b45efe8SAndy Shevchenko 
4294b45efe8SAndy Shevchenko 	ret = intel_lpss_debugfs_add(lpss);
4304b45efe8SAndy Shevchenko 	if (ret)
4314b45efe8SAndy Shevchenko 		dev_warn(dev, "Failed to create debugfs entries\n");
4324b45efe8SAndy Shevchenko 
4334b45efe8SAndy Shevchenko 	if (intel_lpss_has_idma(lpss)) {
4344b45efe8SAndy Shevchenko 		ret = mfd_add_devices(dev, lpss->devid, &intel_lpss_idma64_cell,
4354b45efe8SAndy Shevchenko 				      1, info->mem, info->irq, NULL);
4364b45efe8SAndy Shevchenko 		if (ret)
4374b45efe8SAndy Shevchenko 			dev_warn(dev, "Failed to add %s, fallback to PIO\n",
4384b45efe8SAndy Shevchenko 				 LPSS_IDMA64_DRIVER_NAME);
4394b45efe8SAndy Shevchenko 	}
4404b45efe8SAndy Shevchenko 
4414b45efe8SAndy Shevchenko 	ret = mfd_add_devices(dev, lpss->devid, lpss->cell,
4424b45efe8SAndy Shevchenko 			      1, info->mem, info->irq, NULL);
4434b45efe8SAndy Shevchenko 	if (ret)
4444b45efe8SAndy Shevchenko 		goto err_remove_ltr;
4454b45efe8SAndy Shevchenko 
4468425ec7fSRafael J. Wysocki 	dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
4478425ec7fSRafael J. Wysocki 
4484b45efe8SAndy Shevchenko 	return 0;
4494b45efe8SAndy Shevchenko 
4504b45efe8SAndy Shevchenko err_remove_ltr:
4514b45efe8SAndy Shevchenko 	intel_lpss_debugfs_remove(lpss);
4524b45efe8SAndy Shevchenko 	intel_lpss_ltr_hide(lpss);
45384cb36caSAndy Shevchenko 	intel_lpss_unregister_clock(lpss);
4544b45efe8SAndy Shevchenko 
4554b45efe8SAndy Shevchenko err_clk_register:
456895243c8SChristophe JAILLET 	ida_free(&intel_lpss_devid_ida, lpss->devid);
4574b45efe8SAndy Shevchenko 
4584b45efe8SAndy Shevchenko 	return ret;
4594b45efe8SAndy Shevchenko }
46024ee97a9SAndy Shevchenko EXPORT_SYMBOL_NS_GPL(intel_lpss_probe, INTEL_LPSS);
4614b45efe8SAndy Shevchenko 
4624b45efe8SAndy Shevchenko void intel_lpss_remove(struct device *dev)
4634b45efe8SAndy Shevchenko {
4644b45efe8SAndy Shevchenko 	struct intel_lpss *lpss = dev_get_drvdata(dev);
4654b45efe8SAndy Shevchenko 
4664b45efe8SAndy Shevchenko 	mfd_remove_devices(dev);
4674b45efe8SAndy Shevchenko 	intel_lpss_debugfs_remove(lpss);
4684b45efe8SAndy Shevchenko 	intel_lpss_ltr_hide(lpss);
4694b45efe8SAndy Shevchenko 	intel_lpss_unregister_clock(lpss);
470895243c8SChristophe JAILLET 	ida_free(&intel_lpss_devid_ida, lpss->devid);
4714b45efe8SAndy Shevchenko }
47224ee97a9SAndy Shevchenko EXPORT_SYMBOL_NS_GPL(intel_lpss_remove, INTEL_LPSS);
4734b45efe8SAndy Shevchenko 
4744b45efe8SAndy Shevchenko static int resume_lpss_device(struct device *dev, void *data)
4754b45efe8SAndy Shevchenko {
4768425ec7fSRafael J. Wysocki 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND))
4774b45efe8SAndy Shevchenko 		pm_runtime_resume(dev);
4788425ec7fSRafael J. Wysocki 
4794b45efe8SAndy Shevchenko 	return 0;
4804b45efe8SAndy Shevchenko }
4814b45efe8SAndy Shevchenko 
482fd58bb8cSAndy Shevchenko static int intel_lpss_prepare(struct device *dev)
4834b45efe8SAndy Shevchenko {
4844b45efe8SAndy Shevchenko 	/*
4854b45efe8SAndy Shevchenko 	 * Resume both child devices before entering system sleep. This
4864b45efe8SAndy Shevchenko 	 * ensures that they are in proper state before they get suspended.
4874b45efe8SAndy Shevchenko 	 */
4884b45efe8SAndy Shevchenko 	device_for_each_child_reverse(dev, NULL, resume_lpss_device);
4894b45efe8SAndy Shevchenko 	return 0;
4904b45efe8SAndy Shevchenko }
4914b45efe8SAndy Shevchenko 
492fd58bb8cSAndy Shevchenko static int intel_lpss_suspend(struct device *dev)
4934b45efe8SAndy Shevchenko {
49441a3da2bSHeikki Krogerus 	struct intel_lpss *lpss = dev_get_drvdata(dev);
49541a3da2bSHeikki Krogerus 	unsigned int i;
49641a3da2bSHeikki Krogerus 
49741a3da2bSHeikki Krogerus 	/* Save device context */
49841a3da2bSHeikki Krogerus 	for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
49941a3da2bSHeikki Krogerus 		lpss->priv_ctx[i] = readl(lpss->priv + i * 4);
50041a3da2bSHeikki Krogerus 
5010b471aaaSFurquan Shaikh 	/*
5020b471aaaSFurquan Shaikh 	 * If the device type is not UART, then put the controller into
5030b471aaaSFurquan Shaikh 	 * reset. UART cannot be put into reset since S3/S0ix fail when
5040b471aaaSFurquan Shaikh 	 * no_console_suspend flag is enabled.
5050b471aaaSFurquan Shaikh 	 */
5060b471aaaSFurquan Shaikh 	if (lpss->type != LPSS_DEV_UART)
5070b471aaaSFurquan Shaikh 		writel(0, lpss->priv + LPSS_PRIV_RESETS);
5080b471aaaSFurquan Shaikh 
5094b45efe8SAndy Shevchenko 	return 0;
5104b45efe8SAndy Shevchenko }
5114b45efe8SAndy Shevchenko 
512fd58bb8cSAndy Shevchenko static int intel_lpss_resume(struct device *dev)
5134b45efe8SAndy Shevchenko {
5144b45efe8SAndy Shevchenko 	struct intel_lpss *lpss = dev_get_drvdata(dev);
51541a3da2bSHeikki Krogerus 	unsigned int i;
5164b45efe8SAndy Shevchenko 
51741a3da2bSHeikki Krogerus 	intel_lpss_deassert_reset(lpss);
51841a3da2bSHeikki Krogerus 
51941a3da2bSHeikki Krogerus 	/* Restore device context */
52041a3da2bSHeikki Krogerus 	for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
52141a3da2bSHeikki Krogerus 		writel(lpss->priv_ctx[i], lpss->priv + i * 4);
5224b45efe8SAndy Shevchenko 
5234b45efe8SAndy Shevchenko 	return 0;
5244b45efe8SAndy Shevchenko }
525fd58bb8cSAndy Shevchenko 
526fd58bb8cSAndy Shevchenko EXPORT_NS_GPL_DEV_PM_OPS(intel_lpss_pm_ops, INTEL_LPSS) = {
527fd58bb8cSAndy Shevchenko 	.prepare = pm_sleep_ptr(&intel_lpss_prepare),
528fd58bb8cSAndy Shevchenko 	LATE_SYSTEM_SLEEP_PM_OPS(intel_lpss_suspend, intel_lpss_resume)
529fd58bb8cSAndy Shevchenko 	RUNTIME_PM_OPS(intel_lpss_suspend, intel_lpss_resume, NULL)
530fd58bb8cSAndy Shevchenko };
5314b45efe8SAndy Shevchenko 
5324b45efe8SAndy Shevchenko static int __init intel_lpss_init(void)
5334b45efe8SAndy Shevchenko {
5344b45efe8SAndy Shevchenko 	intel_lpss_debugfs = debugfs_create_dir("intel_lpss", NULL);
5354b45efe8SAndy Shevchenko 	return 0;
5364b45efe8SAndy Shevchenko }
5374b45efe8SAndy Shevchenko module_init(intel_lpss_init);
5384b45efe8SAndy Shevchenko 
5394b45efe8SAndy Shevchenko static void __exit intel_lpss_exit(void)
5404b45efe8SAndy Shevchenko {
54102f36911SAndy Shevchenko 	ida_destroy(&intel_lpss_devid_ida);
5424b45efe8SAndy Shevchenko 	debugfs_remove(intel_lpss_debugfs);
5434b45efe8SAndy Shevchenko }
5444b45efe8SAndy Shevchenko module_exit(intel_lpss_exit);
5454b45efe8SAndy Shevchenko 
5464b45efe8SAndy Shevchenko MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
5474b45efe8SAndy Shevchenko MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
5484b45efe8SAndy Shevchenko MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
5494b45efe8SAndy Shevchenko MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
5504b45efe8SAndy Shevchenko MODULE_DESCRIPTION("Intel LPSS core driver");
5514b45efe8SAndy Shevchenko MODULE_LICENSE("GPL v2");
552569fac74SAndy Shevchenko /*
553569fac74SAndy Shevchenko  * Ensure the DMA driver is loaded before the host controller device appears,
554569fac74SAndy Shevchenko  * so that the host controller driver can request its DMA channels as early
555569fac74SAndy Shevchenko  * as possible.
556569fac74SAndy Shevchenko  *
557569fac74SAndy Shevchenko  * If the DMA module is not there that's OK as well.
558569fac74SAndy Shevchenko  */
559569fac74SAndy Shevchenko MODULE_SOFTDEP("pre: platform:" LPSS_IDMA64_DRIVER_NAME);
560