xref: /linux/drivers/mfd/intel-lpss.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel Sunrisepoint LPSS core support.
4  *
5  * Copyright (C) 2015, Intel Corporation
6  *
7  * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8  *          Mika Westerberg <mika.westerberg@linux.intel.com>
9  *          Heikki Krogerus <heikki.krogerus@linux.intel.com>
10  *          Jarkko Nikula <jarkko.nikula@linux.intel.com>
11  */
12 
13 #include <linux/array_size.h>
14 #include <linux/bits.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk.h>
17 #include <linux/clk-provider.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/gfp_types.h>
22 #include <linux/idr.h>
23 #include <linux/io.h>
24 #include <linux/ioport.h>
25 #include <linux/mfd/core.h>
26 #include <linux/module.h>
27 #include <linux/pm.h>
28 #include <linux/pm_qos.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/sprintf.h>
31 #include <linux/types.h>
32 
33 #include <linux/io-64-nonatomic-lo-hi.h>
34 
35 #include <linux/dma/idma64.h>
36 
37 #include "intel-lpss.h"
38 
39 struct dentry;
40 
41 #define LPSS_DEV_OFFSET		0x000
42 #define LPSS_DEV_SIZE		0x200
43 #define LPSS_PRIV_OFFSET	0x200
44 #define LPSS_PRIV_SIZE		0x100
45 #define LPSS_PRIV_REG_COUNT	(LPSS_PRIV_SIZE / 4)
46 #define LPSS_IDMA64_OFFSET	0x800
47 #define LPSS_IDMA64_SIZE	0x800
48 
49 /* Offsets from lpss->priv */
50 #define LPSS_PRIV_RESETS		0x04
51 #define LPSS_PRIV_RESETS_IDMA		BIT(2)
52 #define LPSS_PRIV_RESETS_FUNC		0x3
53 
54 #define LPSS_PRIV_ACTIVELTR		0x10
55 #define LPSS_PRIV_IDLELTR		0x14
56 
57 #define LPSS_PRIV_LTR_REQ		BIT(15)
58 #define LPSS_PRIV_LTR_SCALE_MASK	GENMASK(11, 10)
59 #define LPSS_PRIV_LTR_SCALE_1US		(2 << 10)
60 #define LPSS_PRIV_LTR_SCALE_32US	(3 << 10)
61 #define LPSS_PRIV_LTR_VALUE_MASK	GENMASK(9, 0)
62 
63 #define LPSS_PRIV_SSP_REG		0x20
64 #define LPSS_PRIV_SSP_REG_DIS_DMA_FIN	BIT(0)
65 
66 #define LPSS_PRIV_REMAP_ADDR		0x40
67 
68 #define LPSS_PRIV_CAPS			0xfc
69 #define LPSS_PRIV_CAPS_NO_IDMA		BIT(8)
70 #define LPSS_PRIV_CAPS_TYPE_MASK	GENMASK(7, 4)
71 #define LPSS_PRIV_CAPS_TYPE_SHIFT	4
72 
73 /* This matches the type field in CAPS register */
74 enum intel_lpss_dev_type {
75 	LPSS_DEV_I2C = 0,
76 	LPSS_DEV_UART,
77 	LPSS_DEV_SPI,
78 };
79 
80 struct intel_lpss {
81 	const struct intel_lpss_platform_info *info;
82 	enum intel_lpss_dev_type type;
83 	struct clk *clk;
84 	struct clk_lookup *clock;
85 	struct mfd_cell *cell;
86 	struct device *dev;
87 	void __iomem *priv;
88 	u32 priv_ctx[LPSS_PRIV_REG_COUNT];
89 	int devid;
90 	u32 caps;
91 	u32 active_ltr;
92 	u32 idle_ltr;
93 	struct dentry *debugfs;
94 };
95 
96 static const struct resource intel_lpss_dev_resources[] = {
97 	DEFINE_RES_MEM_NAMED(LPSS_DEV_OFFSET, LPSS_DEV_SIZE, "lpss_dev"),
98 	DEFINE_RES_MEM_NAMED(LPSS_PRIV_OFFSET, LPSS_PRIV_SIZE, "lpss_priv"),
99 	DEFINE_RES_IRQ(0),
100 };
101 
102 static const struct resource intel_lpss_idma64_resources[] = {
103 	DEFINE_RES_MEM(LPSS_IDMA64_OFFSET, LPSS_IDMA64_SIZE),
104 	DEFINE_RES_IRQ(0),
105 };
106 
107 /*
108  * Cells needs to be ordered so that the iDMA is created first. This is
109  * because we need to be sure the DMA is available when the host controller
110  * driver is probed.
111  */
112 static const struct mfd_cell intel_lpss_idma64_cell = {
113 	.name = LPSS_IDMA64_DRIVER_NAME,
114 	.num_resources = ARRAY_SIZE(intel_lpss_idma64_resources),
115 	.resources = intel_lpss_idma64_resources,
116 };
117 
118 static const struct mfd_cell intel_lpss_i2c_cell = {
119 	.name = "i2c_designware",
120 	.num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
121 	.resources = intel_lpss_dev_resources,
122 };
123 
124 static const struct mfd_cell intel_lpss_uart_cell = {
125 	.name = "dw-apb-uart",
126 	.num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
127 	.resources = intel_lpss_dev_resources,
128 };
129 
130 static const struct mfd_cell intel_lpss_spi_cell = {
131 	.name = "pxa2xx-spi",
132 	.num_resources = ARRAY_SIZE(intel_lpss_dev_resources),
133 	.resources = intel_lpss_dev_resources,
134 };
135 
136 static DEFINE_IDA(intel_lpss_devid_ida);
137 static struct dentry *intel_lpss_debugfs;
138 
139 static void intel_lpss_cache_ltr(struct intel_lpss *lpss)
140 {
141 	lpss->active_ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
142 	lpss->idle_ltr = readl(lpss->priv + LPSS_PRIV_IDLELTR);
143 }
144 
145 static void intel_lpss_debugfs_add(struct intel_lpss *lpss)
146 {
147 	lpss->debugfs = debugfs_create_dir(dev_name(lpss->dev), intel_lpss_debugfs);
148 
149 	/* Cache the values into lpss structure */
150 	intel_lpss_cache_ltr(lpss);
151 
152 	debugfs_create_x32("capabilities", 0444, lpss->debugfs, &lpss->caps);
153 	debugfs_create_x32("active_ltr", 0444, lpss->debugfs, &lpss->active_ltr);
154 	debugfs_create_x32("idle_ltr", 0444, lpss->debugfs, &lpss->idle_ltr);
155 }
156 
157 static void intel_lpss_debugfs_remove(struct intel_lpss *lpss)
158 {
159 	debugfs_remove(lpss->debugfs);
160 }
161 
162 static void intel_lpss_ltr_set(struct device *dev, s32 val)
163 {
164 	struct intel_lpss *lpss = dev_get_drvdata(dev);
165 	u32 ltr;
166 
167 	/*
168 	 * Program latency tolerance (LTR) accordingly what has been asked
169 	 * by the PM QoS layer or disable it in case we were passed
170 	 * negative value or PM_QOS_LATENCY_ANY.
171 	 */
172 	ltr = readl(lpss->priv + LPSS_PRIV_ACTIVELTR);
173 
174 	if (val == PM_QOS_LATENCY_ANY || val < 0) {
175 		ltr &= ~LPSS_PRIV_LTR_REQ;
176 	} else {
177 		ltr |= LPSS_PRIV_LTR_REQ;
178 		ltr &= ~LPSS_PRIV_LTR_SCALE_MASK;
179 		ltr &= ~LPSS_PRIV_LTR_VALUE_MASK;
180 
181 		if (val > LPSS_PRIV_LTR_VALUE_MASK)
182 			ltr |= LPSS_PRIV_LTR_SCALE_32US | val >> 5;
183 		else
184 			ltr |= LPSS_PRIV_LTR_SCALE_1US | val;
185 	}
186 
187 	if (ltr == lpss->active_ltr)
188 		return;
189 
190 	writel(ltr, lpss->priv + LPSS_PRIV_ACTIVELTR);
191 	writel(ltr, lpss->priv + LPSS_PRIV_IDLELTR);
192 
193 	/* Cache the values into lpss structure */
194 	intel_lpss_cache_ltr(lpss);
195 }
196 
197 static void intel_lpss_ltr_expose(struct intel_lpss *lpss)
198 {
199 	lpss->dev->power.set_latency_tolerance = intel_lpss_ltr_set;
200 	dev_pm_qos_expose_latency_tolerance(lpss->dev);
201 }
202 
203 static void intel_lpss_ltr_hide(struct intel_lpss *lpss)
204 {
205 	dev_pm_qos_hide_latency_tolerance(lpss->dev);
206 	lpss->dev->power.set_latency_tolerance = NULL;
207 }
208 
209 static int intel_lpss_assign_devs(struct intel_lpss *lpss)
210 {
211 	const struct mfd_cell *cell;
212 	unsigned int type;
213 
214 	type = lpss->caps & LPSS_PRIV_CAPS_TYPE_MASK;
215 	type >>= LPSS_PRIV_CAPS_TYPE_SHIFT;
216 
217 	switch (type) {
218 	case LPSS_DEV_I2C:
219 		cell = &intel_lpss_i2c_cell;
220 		break;
221 	case LPSS_DEV_UART:
222 		cell = &intel_lpss_uart_cell;
223 		break;
224 	case LPSS_DEV_SPI:
225 		cell = &intel_lpss_spi_cell;
226 		break;
227 	default:
228 		return -ENODEV;
229 	}
230 
231 	lpss->cell = devm_kmemdup(lpss->dev, cell, sizeof(*cell), GFP_KERNEL);
232 	if (!lpss->cell)
233 		return -ENOMEM;
234 
235 	lpss->type = type;
236 
237 	return 0;
238 }
239 
240 static bool intel_lpss_has_idma(const struct intel_lpss *lpss)
241 {
242 	return (lpss->caps & LPSS_PRIV_CAPS_NO_IDMA) == 0;
243 }
244 
245 static void intel_lpss_set_remap_addr(const struct intel_lpss *lpss)
246 {
247 	resource_size_t addr = lpss->info->mem->start;
248 
249 	lo_hi_writeq(addr, lpss->priv + LPSS_PRIV_REMAP_ADDR);
250 }
251 
252 static void intel_lpss_deassert_reset(const struct intel_lpss *lpss)
253 {
254 	u32 value = LPSS_PRIV_RESETS_FUNC | LPSS_PRIV_RESETS_IDMA;
255 
256 	/* Bring out the device from reset */
257 	writel(value, lpss->priv + LPSS_PRIV_RESETS);
258 }
259 
260 static void intel_lpss_init_dev(const struct intel_lpss *lpss)
261 {
262 	u32 value = LPSS_PRIV_SSP_REG_DIS_DMA_FIN;
263 
264 	/* Set the device in reset state */
265 	writel(0, lpss->priv + LPSS_PRIV_RESETS);
266 
267 	intel_lpss_deassert_reset(lpss);
268 
269 	intel_lpss_set_remap_addr(lpss);
270 
271 	if (!intel_lpss_has_idma(lpss))
272 		return;
273 
274 	/* Make sure that SPI multiblock DMA transfers are re-enabled */
275 	if (lpss->type == LPSS_DEV_SPI)
276 		writel(value, lpss->priv + LPSS_PRIV_SSP_REG);
277 }
278 
279 static void intel_lpss_unregister_clock_tree(struct clk *clk)
280 {
281 	struct clk *parent;
282 
283 	while (clk) {
284 		parent = clk_get_parent(clk);
285 		clk_unregister(clk);
286 		clk = parent;
287 	}
288 }
289 
290 static int intel_lpss_register_clock_divider(struct intel_lpss *lpss,
291 					     const char *devname,
292 					     struct clk **clk)
293 {
294 	char name[32];
295 	struct clk *tmp = *clk;
296 	int ret;
297 
298 	snprintf(name, sizeof(name), "%s-enable", devname);
299 	tmp = clk_register_gate(NULL, name, __clk_get_name(tmp), 0,
300 				lpss->priv, 0, 0, NULL);
301 	if (IS_ERR(tmp))
302 		return PTR_ERR(tmp);
303 
304 	snprintf(name, sizeof(name), "%s-div", devname);
305 	tmp = clk_register_fractional_divider(NULL, name, __clk_get_name(tmp),
306 					      0, lpss->priv, 1, 15, 16, 15,
307 					      CLK_FRAC_DIVIDER_POWER_OF_TWO_PS,
308 					      NULL);
309 	if (IS_ERR(tmp))
310 		return PTR_ERR(tmp);
311 	*clk = tmp;
312 
313 	if (lpss->info->quirks & QUIRK_CLOCK_DIVIDER_UNITY) {
314 		ret = clk_set_rate(tmp, lpss->info->clk_rate);
315 		if (ret)
316 			return ret;
317 	}
318 
319 	snprintf(name, sizeof(name), "%s-update", devname);
320 	tmp = clk_register_gate(NULL, name, __clk_get_name(tmp),
321 				CLK_SET_RATE_PARENT, lpss->priv, 31, 0, NULL);
322 	if (IS_ERR(tmp))
323 		return PTR_ERR(tmp);
324 	*clk = tmp;
325 
326 	return 0;
327 }
328 
329 static int intel_lpss_register_clock(struct intel_lpss *lpss)
330 {
331 	const struct mfd_cell *cell = lpss->cell;
332 	struct clk *clk;
333 	char devname[24];
334 	int ret;
335 
336 	if (!lpss->info->clk_rate)
337 		return 0;
338 
339 	/* Root clock */
340 	clk = clk_register_fixed_rate(NULL, dev_name(lpss->dev), NULL, 0,
341 				      lpss->info->clk_rate);
342 	if (IS_ERR(clk))
343 		return PTR_ERR(clk);
344 
345 	snprintf(devname, sizeof(devname), "%s.%d", cell->name, lpss->devid);
346 
347 	/*
348 	 * Support for clock divider only if it has some preset value.
349 	 * Otherwise we assume that the divider is not used.
350 	 */
351 	if (lpss->type != LPSS_DEV_I2C) {
352 		ret = intel_lpss_register_clock_divider(lpss, devname, &clk);
353 		if (ret)
354 			goto err_clk_register;
355 	}
356 
357 	ret = -ENOMEM;
358 
359 	/* Clock for the host controller */
360 	lpss->clock = clkdev_create(clk, lpss->info->clk_con_id, "%s", devname);
361 	if (!lpss->clock)
362 		goto err_clk_register;
363 
364 	lpss->clk = clk;
365 
366 	return 0;
367 
368 err_clk_register:
369 	intel_lpss_unregister_clock_tree(clk);
370 
371 	return ret;
372 }
373 
374 static void intel_lpss_unregister_clock(struct intel_lpss *lpss)
375 {
376 	if (IS_ERR_OR_NULL(lpss->clk))
377 		return;
378 
379 	clkdev_drop(lpss->clock);
380 	intel_lpss_unregister_clock_tree(lpss->clk);
381 }
382 
383 int intel_lpss_probe(struct device *dev,
384 		     const struct intel_lpss_platform_info *info)
385 {
386 	struct intel_lpss *lpss;
387 	int ret;
388 
389 	if (!info || !info->mem)
390 		return -EINVAL;
391 
392 	if (info->irq < 0)
393 		return info->irq;
394 
395 	lpss = devm_kzalloc_obj(dev, *lpss);
396 	if (!lpss)
397 		return -ENOMEM;
398 
399 	lpss->priv = devm_ioremap_uc(dev, info->mem->start + LPSS_PRIV_OFFSET,
400 				  LPSS_PRIV_SIZE);
401 	if (!lpss->priv)
402 		return -ENOMEM;
403 
404 	lpss->info = info;
405 	lpss->dev = dev;
406 	lpss->caps = readl(lpss->priv + LPSS_PRIV_CAPS);
407 
408 	dev_set_drvdata(dev, lpss);
409 
410 	ret = intel_lpss_assign_devs(lpss);
411 	if (ret)
412 		return ret;
413 
414 	lpss->cell->swnode = info->swnode;
415 	lpss->cell->ignore_resource_conflicts = info->quirks & QUIRK_IGNORE_RESOURCE_CONFLICTS;
416 
417 	intel_lpss_init_dev(lpss);
418 
419 	lpss->devid = ida_alloc(&intel_lpss_devid_ida, GFP_KERNEL);
420 	if (lpss->devid < 0)
421 		return lpss->devid;
422 
423 	ret = intel_lpss_register_clock(lpss);
424 	if (ret)
425 		goto err_clk_register;
426 
427 	intel_lpss_ltr_expose(lpss);
428 	intel_lpss_debugfs_add(lpss);
429 
430 	if (intel_lpss_has_idma(lpss)) {
431 		ret = mfd_add_devices(dev, lpss->devid, &intel_lpss_idma64_cell,
432 				      1, info->mem, info->irq, NULL);
433 		if (ret)
434 			dev_warn(dev, "Failed to add %s, fallback to PIO\n",
435 				 LPSS_IDMA64_DRIVER_NAME);
436 	}
437 
438 	ret = mfd_add_devices(dev, lpss->devid, lpss->cell,
439 			      1, info->mem, info->irq, NULL);
440 	if (ret)
441 		goto err_remove_ltr;
442 
443 	dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
444 
445 	return 0;
446 
447 err_remove_ltr:
448 	intel_lpss_debugfs_remove(lpss);
449 	intel_lpss_ltr_hide(lpss);
450 	intel_lpss_unregister_clock(lpss);
451 
452 err_clk_register:
453 	ida_free(&intel_lpss_devid_ida, lpss->devid);
454 
455 	return ret;
456 }
457 EXPORT_SYMBOL_NS_GPL(intel_lpss_probe, "INTEL_LPSS");
458 
459 void intel_lpss_remove(struct device *dev)
460 {
461 	struct intel_lpss *lpss = dev_get_drvdata(dev);
462 
463 	mfd_remove_devices(dev);
464 	intel_lpss_debugfs_remove(lpss);
465 	intel_lpss_ltr_hide(lpss);
466 	intel_lpss_unregister_clock(lpss);
467 	ida_free(&intel_lpss_devid_ida, lpss->devid);
468 }
469 EXPORT_SYMBOL_NS_GPL(intel_lpss_remove, "INTEL_LPSS");
470 
471 static int resume_lpss_device(struct device *dev, void *data)
472 {
473 	if (!dev_pm_smart_suspend(dev))
474 		pm_runtime_resume(dev);
475 
476 	return 0;
477 }
478 
479 static int intel_lpss_prepare(struct device *dev)
480 {
481 	/*
482 	 * Resume both child devices before entering system sleep. This
483 	 * ensures that they are in proper state before they get suspended.
484 	 */
485 	device_for_each_child_reverse(dev, NULL, resume_lpss_device);
486 	return 0;
487 }
488 
489 static int intel_lpss_suspend(struct device *dev)
490 {
491 	struct intel_lpss *lpss = dev_get_drvdata(dev);
492 	unsigned int i;
493 
494 	/* Save device context */
495 	for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
496 		lpss->priv_ctx[i] = readl(lpss->priv + i * 4);
497 
498 	/*
499 	 * If the device type is not UART, then put the controller into
500 	 * reset. UART cannot be put into reset since S3/S0ix fail when
501 	 * no_console_suspend flag is enabled.
502 	 */
503 	if (lpss->type != LPSS_DEV_UART)
504 		writel(0, lpss->priv + LPSS_PRIV_RESETS);
505 
506 	return 0;
507 }
508 
509 static int intel_lpss_resume(struct device *dev)
510 {
511 	struct intel_lpss *lpss = dev_get_drvdata(dev);
512 	unsigned int i;
513 
514 	intel_lpss_deassert_reset(lpss);
515 
516 	/* Restore device context */
517 	for (i = 0; i < LPSS_PRIV_REG_COUNT; i++)
518 		writel(lpss->priv_ctx[i], lpss->priv + i * 4);
519 
520 	return 0;
521 }
522 
523 EXPORT_NS_GPL_DEV_PM_OPS(intel_lpss_pm_ops, INTEL_LPSS) = {
524 	.prepare = pm_sleep_ptr(&intel_lpss_prepare),
525 	LATE_SYSTEM_SLEEP_PM_OPS(intel_lpss_suspend, intel_lpss_resume)
526 	RUNTIME_PM_OPS(intel_lpss_suspend, intel_lpss_resume, NULL)
527 };
528 
529 static int __init intel_lpss_init(void)
530 {
531 	intel_lpss_debugfs = debugfs_create_dir("intel_lpss", NULL);
532 	return 0;
533 }
534 module_init(intel_lpss_init);
535 
536 static void __exit intel_lpss_exit(void)
537 {
538 	ida_destroy(&intel_lpss_devid_ida);
539 	debugfs_remove(intel_lpss_debugfs);
540 }
541 module_exit(intel_lpss_exit);
542 
543 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
544 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
545 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
546 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
547 MODULE_DESCRIPTION("Intel LPSS core driver");
548 MODULE_LICENSE("GPL v2");
549 /*
550  * Ensure the DMA driver is loaded before the host controller device appears,
551  * so that the host controller driver can request its DMA channels as early
552  * as possible.
553  *
554  * If the DMA module is not there that's OK as well.
555  */
556 MODULE_SOFTDEP("pre: platform:" LPSS_IDMA64_DRIVER_NAME);
557