Merge remote-tracking branches 'regulator/topic/notifier', 'regulator/topic/pfuze100...
[sfrench/cifs-2.6.git] / drivers / tty / serial / 8250 / 8250_pxa.c
1 /*
2  *  drivers/tty/serial/8250/8250_pxa.c -- driver for PXA on-board UARTS
3  *  Copyright:  (C) 2013 Sergei Ianovich <ynvich@gmail.com>
4  *
5  *  replaces drivers/serial/pxa.c by Nicolas Pitre
6  *  Created:    Feb 20, 2003
7  *  Copyright:  (C) 2003 Monta Vista Software, Inc.
8  *
9  *  Based on drivers/serial/8250.c by Russell King.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  */
17
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/io.h>
21 #include <linux/module.h>
22 #include <linux/serial_8250.h>
23 #include <linux/serial_core.h>
24 #include <linux/serial_reg.h>
25 #include <linux/of.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_platform.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/clk.h>
31 #include <linux/pm_runtime.h>
32
33 #include "8250.h"
34
35 struct pxa8250_data {
36         int                     line;
37         struct clk              *clk;
38 };
39
40 static int __maybe_unused serial_pxa_suspend(struct device *dev)
41 {
42         struct pxa8250_data *data = dev_get_drvdata(dev);
43
44         serial8250_suspend_port(data->line);
45
46         return 0;
47 }
48
49 static int __maybe_unused serial_pxa_resume(struct device *dev)
50 {
51         struct pxa8250_data *data = dev_get_drvdata(dev);
52
53         serial8250_resume_port(data->line);
54
55         return 0;
56 }
57
58 static const struct dev_pm_ops serial_pxa_pm_ops = {
59         SET_SYSTEM_SLEEP_PM_OPS(serial_pxa_suspend, serial_pxa_resume)
60 };
61
62 static const struct of_device_id serial_pxa_dt_ids[] = {
63         { .compatible = "mrvl,pxa-uart", },
64         { .compatible = "mrvl,mmp-uart", },
65         {}
66 };
67 MODULE_DEVICE_TABLE(of, serial_pxa_dt_ids);
68
69 /* Uart divisor latch write */
70 static void serial_pxa_dl_write(struct uart_8250_port *up, int value)
71 {
72         unsigned int dll;
73
74         serial_out(up, UART_DLL, value & 0xff);
75         /*
76          * work around Erratum #74 according to Marvel(R) PXA270M Processor
77          * Specification Update (April 19, 2010)
78          */
79         dll = serial_in(up, UART_DLL);
80         WARN_ON(dll != (value & 0xff));
81
82         serial_out(up, UART_DLM, value >> 8 & 0xff);
83 }
84
85
86 static void serial_pxa_pm(struct uart_port *port, unsigned int state,
87               unsigned int oldstate)
88 {
89         struct pxa8250_data *data = port->private_data;
90
91         if (!state)
92                 clk_prepare_enable(data->clk);
93         else
94                 clk_disable_unprepare(data->clk);
95 }
96
97 static int serial_pxa_probe(struct platform_device *pdev)
98 {
99         struct uart_8250_port uart = {};
100         struct pxa8250_data *data;
101         struct resource *mmres, *irqres;
102         int ret;
103
104         mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105         irqres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
106         if (!mmres || !irqres)
107                 return -ENODEV;
108
109         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
110         if (!data)
111                 return -ENOMEM;
112
113         data->clk = devm_clk_get(&pdev->dev, NULL);
114         if (IS_ERR(data->clk))
115                 return PTR_ERR(data->clk);
116
117         ret = clk_prepare(data->clk);
118         if (ret)
119                 return ret;
120
121         uart.port.type = PORT_XSCALE;
122         uart.port.iotype = UPIO_MEM32;
123         uart.port.mapbase = mmres->start;
124         uart.port.regshift = 2;
125         uart.port.irq = irqres->start;
126         uart.port.fifosize = 64;
127         uart.port.flags = UPF_IOREMAP | UPF_SKIP_TEST;
128         uart.port.dev = &pdev->dev;
129         uart.port.uartclk = clk_get_rate(data->clk);
130         uart.port.pm = serial_pxa_pm;
131         uart.port.private_data = data;
132         uart.dl_write = serial_pxa_dl_write;
133
134         ret = serial8250_register_8250_port(&uart);
135         if (ret < 0)
136                 goto err_clk;
137
138         data->line = ret;
139
140         platform_set_drvdata(pdev, data);
141
142         return 0;
143
144  err_clk:
145         clk_unprepare(data->clk);
146         return ret;
147 }
148
149 static int serial_pxa_remove(struct platform_device *pdev)
150 {
151         struct pxa8250_data *data = platform_get_drvdata(pdev);
152
153         serial8250_unregister_port(data->line);
154
155         clk_unprepare(data->clk);
156
157         return 0;
158 }
159
160 static struct platform_driver serial_pxa_driver = {
161         .probe          = serial_pxa_probe,
162         .remove         = serial_pxa_remove,
163
164         .driver         = {
165                 .name   = "pxa2xx-uart",
166                 .pm     = &serial_pxa_pm_ops,
167                 .of_match_table = serial_pxa_dt_ids,
168         },
169 };
170
171 module_platform_driver(serial_pxa_driver);
172
173 #ifdef CONFIG_SERIAL_8250_CONSOLE
174 static int __init early_serial_pxa_setup(struct earlycon_device *device,
175                                   const char *options)
176 {
177         struct uart_port *port = &device->port;
178
179         if (!(device->port.membase || device->port.iobase))
180                 return -ENODEV;
181
182         port->regshift = 2;
183         return early_serial8250_setup(device, NULL);
184 }
185 OF_EARLYCON_DECLARE(early_pxa, "mrvl,pxa-uart", early_serial_pxa_setup);
186 #endif
187
188 MODULE_AUTHOR("Sergei Ianovich");
189 MODULE_LICENSE("GPL");
190 MODULE_ALIAS("platform:pxa2xx-uart");