fb78bb9ad8f65888817fc6f7a3ecb2563b975134
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / acpi / acpi_lpss.c
1 /*
2  * ACPI support for Intel Lynxpoint LPSS.
3  *
4  * Copyright (C) 2013, Intel Corporation
5  * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
6  *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/acpi.h>
14 #include <linux/clk.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk-provider.h>
17 #include <linux/err.h>
18 #include <linux/io.h>
19 #include <linux/platform_device.h>
20 #include <linux/platform_data/clk-lpss.h>
21 #include <linux/pm_runtime.h>
22
23 #include "internal.h"
24
25 ACPI_MODULE_NAME("acpi_lpss");
26
27 #define LPSS_CLK_SIZE   0x04
28 #define LPSS_LTR_SIZE   0x18
29
30 /* Offsets relative to LPSS_PRIVATE_OFFSET */
31 #define LPSS_GENERAL                    0x08
32 #define LPSS_GENERAL_LTR_MODE_SW        BIT(2)
33 #define LPSS_SW_LTR                     0x10
34 #define LPSS_AUTO_LTR                   0x14
35 #define LPSS_TX_INT                     0x20
36 #define LPSS_TX_INT_MASK                BIT(1)
37
38 struct lpss_shared_clock {
39         const char *name;
40         unsigned long rate;
41         struct clk *clk;
42 };
43
44 struct lpss_private_data;
45
46 struct lpss_device_desc {
47         bool clk_required;
48         const char *clkdev_name;
49         bool ltr_required;
50         unsigned int prv_offset;
51         size_t prv_size_override;
52         bool clk_gate;
53         struct lpss_shared_clock *shared_clock;
54         void (*setup)(struct lpss_private_data *pdata);
55 };
56
57 static struct lpss_device_desc lpss_dma_desc = {
58         .clk_required = true,
59         .clkdev_name = "hclk",
60 };
61
62 struct lpss_private_data {
63         void __iomem *mmio_base;
64         resource_size_t mmio_size;
65         struct clk *clk;
66         const struct lpss_device_desc *dev_desc;
67 };
68
69 static void lpss_uart_setup(struct lpss_private_data *pdata)
70 {
71         unsigned int tx_int_offset = pdata->dev_desc->prv_offset + LPSS_TX_INT;
72         u32 reg;
73
74         reg = readl(pdata->mmio_base + tx_int_offset);
75         writel(reg | LPSS_TX_INT_MASK, pdata->mmio_base + tx_int_offset);
76 }
77
78 static struct lpss_device_desc lpt_dev_desc = {
79         .clk_required = true,
80         .prv_offset = 0x800,
81         .ltr_required = true,
82         .clk_gate = true,
83 };
84
85 static struct lpss_device_desc lpt_uart_dev_desc = {
86         .clk_required = true,
87         .prv_offset = 0x800,
88         .ltr_required = true,
89         .clk_gate = true,
90         .setup = lpss_uart_setup,
91 };
92
93 static struct lpss_device_desc lpt_sdio_dev_desc = {
94         .prv_offset = 0x1000,
95         .prv_size_override = 0x1018,
96         .ltr_required = true,
97 };
98
99 static struct lpss_shared_clock uart_clock = {
100         .name = "uart_clk",
101         .rate = 44236800,
102 };
103
104 static struct lpss_device_desc byt_uart_dev_desc = {
105         .clk_required = true,
106         .prv_offset = 0x800,
107         .clk_gate = true,
108         .shared_clock = &uart_clock,
109         .setup = lpss_uart_setup,
110 };
111
112 static struct lpss_shared_clock spi_clock = {
113         .name = "spi_clk",
114         .rate = 50000000,
115 };
116
117 static struct lpss_device_desc byt_spi_dev_desc = {
118         .clk_required = true,
119         .prv_offset = 0x400,
120         .clk_gate = true,
121         .shared_clock = &spi_clock,
122 };
123
124 static struct lpss_device_desc byt_sdio_dev_desc = {
125         .clk_required = true,
126 };
127
128 static struct lpss_shared_clock i2c_clock = {
129         .name = "i2c_clk",
130         .rate = 100000000,
131 };
132
133 static struct lpss_device_desc byt_i2c_dev_desc = {
134         .clk_required = true,
135         .prv_offset = 0x800,
136         .shared_clock = &i2c_clock,
137 };
138
139 static const struct acpi_device_id acpi_lpss_device_ids[] = {
140         /* Generic LPSS devices */
141         { "INTL9C60", (unsigned long)&lpss_dma_desc },
142
143         /* Lynxpoint LPSS devices */
144         { "INT33C0", (unsigned long)&lpt_dev_desc },
145         { "INT33C1", (unsigned long)&lpt_dev_desc },
146         { "INT33C2", (unsigned long)&lpt_dev_desc },
147         { "INT33C3", (unsigned long)&lpt_dev_desc },
148         { "INT33C4", (unsigned long)&lpt_uart_dev_desc },
149         { "INT33C5", (unsigned long)&lpt_uart_dev_desc },
150         { "INT33C6", (unsigned long)&lpt_sdio_dev_desc },
151         { "INT33C7", },
152
153         /* BayTrail LPSS devices */
154         { "80860F0A", (unsigned long)&byt_uart_dev_desc },
155         { "80860F0E", (unsigned long)&byt_spi_dev_desc },
156         { "80860F14", (unsigned long)&byt_sdio_dev_desc },
157         { "80860F41", (unsigned long)&byt_i2c_dev_desc },
158         { "INT33B2", },
159
160         { }
161 };
162
163 static int is_memory(struct acpi_resource *res, void *not_used)
164 {
165         struct resource r;
166         return !acpi_dev_resource_memory(res, &r);
167 }
168
169 /* LPSS main clock device. */
170 static struct platform_device *lpss_clk_dev;
171
172 static inline void lpt_register_clock_device(void)
173 {
174         lpss_clk_dev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
175 }
176
177 static int register_device_clock(struct acpi_device *adev,
178                                  struct lpss_private_data *pdata)
179 {
180         const struct lpss_device_desc *dev_desc = pdata->dev_desc;
181         struct lpss_shared_clock *shared_clock = dev_desc->shared_clock;
182         struct clk *clk = ERR_PTR(-ENODEV);
183         struct lpss_clk_data *clk_data;
184         const char *parent;
185
186         if (!lpss_clk_dev)
187                 lpt_register_clock_device();
188
189         clk_data = platform_get_drvdata(lpss_clk_dev);
190         if (!clk_data)
191                 return -ENODEV;
192
193         if (dev_desc->clkdev_name) {
194                 clk_register_clkdev(clk_data->clk, dev_desc->clkdev_name,
195                                     dev_name(&adev->dev));
196                 return 0;
197         }
198
199         if (!pdata->mmio_base
200             || pdata->mmio_size < dev_desc->prv_offset + LPSS_CLK_SIZE)
201                 return -ENODATA;
202
203         parent = clk_data->name;
204
205         if (shared_clock) {
206                 clk = shared_clock->clk;
207                 if (!clk) {
208                         clk = clk_register_fixed_rate(NULL, shared_clock->name,
209                                                       "lpss_clk", 0,
210                                                       shared_clock->rate);
211                         shared_clock->clk = clk;
212                 }
213                 parent = shared_clock->name;
214         }
215
216         if (dev_desc->clk_gate) {
217                 clk = clk_register_gate(NULL, dev_name(&adev->dev), parent, 0,
218                                         pdata->mmio_base + dev_desc->prv_offset,
219                                         0, 0, NULL);
220                 pdata->clk = clk;
221         }
222
223         if (IS_ERR(clk))
224                 return PTR_ERR(clk);
225
226         clk_register_clkdev(clk, NULL, dev_name(&adev->dev));
227         return 0;
228 }
229
230 static int acpi_lpss_create_device(struct acpi_device *adev,
231                                    const struct acpi_device_id *id)
232 {
233         struct lpss_device_desc *dev_desc;
234         struct lpss_private_data *pdata;
235         struct resource_list_entry *rentry;
236         struct list_head resource_list;
237         int ret;
238
239         dev_desc = (struct lpss_device_desc *)id->driver_data;
240         if (!dev_desc)
241                 return acpi_create_platform_device(adev, id);
242
243         pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
244         if (!pdata)
245                 return -ENOMEM;
246
247         INIT_LIST_HEAD(&resource_list);
248         ret = acpi_dev_get_resources(adev, &resource_list, is_memory, NULL);
249         if (ret < 0)
250                 goto err_out;
251
252         list_for_each_entry(rentry, &resource_list, node)
253                 if (resource_type(&rentry->res) == IORESOURCE_MEM) {
254                         if (dev_desc->prv_size_override)
255                                 pdata->mmio_size = dev_desc->prv_size_override;
256                         else
257                                 pdata->mmio_size = resource_size(&rentry->res);
258                         pdata->mmio_base = ioremap(rentry->res.start,
259                                                    pdata->mmio_size);
260                         break;
261                 }
262
263         acpi_dev_free_resource_list(&resource_list);
264
265         pdata->dev_desc = dev_desc;
266
267         if (dev_desc->clk_required) {
268                 ret = register_device_clock(adev, pdata);
269                 if (ret) {
270                         /* Skip the device, but continue the namespace scan. */
271                         ret = 0;
272                         goto err_out;
273                 }
274         }
275
276         /*
277          * This works around a known issue in ACPI tables where LPSS devices
278          * have _PS0 and _PS3 without _PSC (and no power resources), so
279          * acpi_bus_init_power() will assume that the BIOS has put them into D0.
280          */
281         ret = acpi_device_fix_up_power(adev);
282         if (ret) {
283                 /* Skip the device, but continue the namespace scan. */
284                 ret = 0;
285                 goto err_out;
286         }
287
288         if (dev_desc->setup)
289                 dev_desc->setup(pdata);
290
291         adev->driver_data = pdata;
292         ret = acpi_create_platform_device(adev, id);
293         if (ret > 0)
294                 return ret;
295
296         adev->driver_data = NULL;
297
298  err_out:
299         kfree(pdata);
300         return ret;
301 }
302
303 static int lpss_reg_read(struct device *dev, unsigned int reg, u32 *val)
304 {
305         struct acpi_device *adev;
306         struct lpss_private_data *pdata;
307         unsigned long flags;
308         int ret;
309
310         ret = acpi_bus_get_device(ACPI_HANDLE(dev), &adev);
311         if (WARN_ON(ret))
312                 return ret;
313
314         spin_lock_irqsave(&dev->power.lock, flags);
315         if (pm_runtime_suspended(dev)) {
316                 ret = -EAGAIN;
317                 goto out;
318         }
319         pdata = acpi_driver_data(adev);
320         if (WARN_ON(!pdata || !pdata->mmio_base)) {
321                 ret = -ENODEV;
322                 goto out;
323         }
324         *val = readl(pdata->mmio_base + pdata->dev_desc->prv_offset + reg);
325
326  out:
327         spin_unlock_irqrestore(&dev->power.lock, flags);
328         return ret;
329 }
330
331 static ssize_t lpss_ltr_show(struct device *dev, struct device_attribute *attr,
332                              char *buf)
333 {
334         u32 ltr_value = 0;
335         unsigned int reg;
336         int ret;
337
338         reg = strcmp(attr->attr.name, "auto_ltr") ? LPSS_SW_LTR : LPSS_AUTO_LTR;
339         ret = lpss_reg_read(dev, reg, &ltr_value);
340         if (ret)
341                 return ret;
342
343         return snprintf(buf, PAGE_SIZE, "%08x\n", ltr_value);
344 }
345
346 static ssize_t lpss_ltr_mode_show(struct device *dev,
347                                   struct device_attribute *attr, char *buf)
348 {
349         u32 ltr_mode = 0;
350         char *outstr;
351         int ret;
352
353         ret = lpss_reg_read(dev, LPSS_GENERAL, &ltr_mode);
354         if (ret)
355                 return ret;
356
357         outstr = (ltr_mode & LPSS_GENERAL_LTR_MODE_SW) ? "sw" : "auto";
358         return sprintf(buf, "%s\n", outstr);
359 }
360
361 static DEVICE_ATTR(auto_ltr, S_IRUSR, lpss_ltr_show, NULL);
362 static DEVICE_ATTR(sw_ltr, S_IRUSR, lpss_ltr_show, NULL);
363 static DEVICE_ATTR(ltr_mode, S_IRUSR, lpss_ltr_mode_show, NULL);
364
365 static struct attribute *lpss_attrs[] = {
366         &dev_attr_auto_ltr.attr,
367         &dev_attr_sw_ltr.attr,
368         &dev_attr_ltr_mode.attr,
369         NULL,
370 };
371
372 static struct attribute_group lpss_attr_group = {
373         .attrs = lpss_attrs,
374         .name = "lpss_ltr",
375 };
376
377 static int acpi_lpss_platform_notify(struct notifier_block *nb,
378                                      unsigned long action, void *data)
379 {
380         struct platform_device *pdev = to_platform_device(data);
381         struct lpss_private_data *pdata;
382         struct acpi_device *adev;
383         const struct acpi_device_id *id;
384         int ret = 0;
385
386         id = acpi_match_device(acpi_lpss_device_ids, &pdev->dev);
387         if (!id || !id->driver_data)
388                 return 0;
389
390         if (acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
391                 return 0;
392
393         pdata = acpi_driver_data(adev);
394         if (!pdata || !pdata->mmio_base || !pdata->dev_desc->ltr_required)
395                 return 0;
396
397         if (pdata->mmio_size < pdata->dev_desc->prv_offset + LPSS_LTR_SIZE) {
398                 dev_err(&pdev->dev, "MMIO size insufficient to access LTR\n");
399                 return 0;
400         }
401
402         if (action == BUS_NOTIFY_ADD_DEVICE)
403                 ret = sysfs_create_group(&pdev->dev.kobj, &lpss_attr_group);
404         else if (action == BUS_NOTIFY_DEL_DEVICE)
405                 sysfs_remove_group(&pdev->dev.kobj, &lpss_attr_group);
406
407         return ret;
408 }
409
410 static struct notifier_block acpi_lpss_nb = {
411         .notifier_call = acpi_lpss_platform_notify,
412 };
413
414 static struct acpi_scan_handler lpss_handler = {
415         .ids = acpi_lpss_device_ids,
416         .attach = acpi_lpss_create_device,
417 };
418
419 void __init acpi_lpss_init(void)
420 {
421         if (!lpt_clk_init()) {
422                 bus_register_notifier(&platform_bus_type, &acpi_lpss_nb);
423                 acpi_scan_add_handler(&lpss_handler);
424         }
425 }