1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b6d6454fSBen Dooks /* linux/drivers/mfd/sm501.c
3b6d6454fSBen Dooks *
4b6d6454fSBen Dooks * Copyright (C) 2006 Simtec Electronics
5b6d6454fSBen Dooks * Ben Dooks <ben@simtec.co.uk>
6b6d6454fSBen Dooks * Vincent Sanders <vince@simtec.co.uk>
7b6d6454fSBen Dooks *
8b6d6454fSBen Dooks * SM501 MFD driver
9b6d6454fSBen Dooks */
10b6d6454fSBen Dooks
11b6d6454fSBen Dooks #include <linux/kernel.h>
12b6d6454fSBen Dooks #include <linux/module.h>
13b6d6454fSBen Dooks #include <linux/delay.h>
14b6d6454fSBen Dooks #include <linux/init.h>
15b6d6454fSBen Dooks #include <linux/list.h>
16b6d6454fSBen Dooks #include <linux/device.h>
17b6d6454fSBen Dooks #include <linux/platform_device.h>
18b6d6454fSBen Dooks #include <linux/pci.h>
191e9d4219SWolfram Sang #include <linux/platform_data/i2c-gpio.h>
20f68c0a87SLinus Walleij #include <linux/gpio/driver.h>
21b2e63555SLinus Walleij #include <linux/gpio/machine.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
23b6d6454fSBen Dooks
24b6d6454fSBen Dooks #include <linux/sm501.h>
25b6d6454fSBen Dooks #include <linux/sm501-regs.h>
2661711f8fSMagnus Damm #include <linux/serial_8250.h>
27b6d6454fSBen Dooks
28f77401d4SAxel Lin #include <linux/io.h>
29b6d6454fSBen Dooks
30b6d6454fSBen Dooks struct sm501_device {
31b6d6454fSBen Dooks struct list_head list;
32b6d6454fSBen Dooks struct platform_device pdev;
33b6d6454fSBen Dooks };
34b6d6454fSBen Dooks
35f61be273SBen Dooks struct sm501_gpio;
36f61be273SBen Dooks
37f2999209SBen Dooks #ifdef CONFIG_MFD_SM501_GPIO
38f2999209SBen Dooks #include <linux/gpio.h>
39f2999209SBen Dooks
40f61be273SBen Dooks struct sm501_gpio_chip {
41f61be273SBen Dooks struct gpio_chip gpio;
42f61be273SBen Dooks struct sm501_gpio *ourgpio; /* to get back to parent. */
43f61be273SBen Dooks void __iomem *regbase;
4498325f8fSBen Dooks void __iomem *control; /* address of control reg. */
45f61be273SBen Dooks };
46f61be273SBen Dooks
47f61be273SBen Dooks struct sm501_gpio {
48f61be273SBen Dooks struct sm501_gpio_chip low;
49f61be273SBen Dooks struct sm501_gpio_chip high;
50f61be273SBen Dooks spinlock_t lock;
51f61be273SBen Dooks
52f61be273SBen Dooks unsigned int registered : 1;
53f61be273SBen Dooks void __iomem *regs;
54f61be273SBen Dooks struct resource *regs_res;
55f61be273SBen Dooks };
56f2999209SBen Dooks #else
57f2999209SBen Dooks struct sm501_gpio {
58f2999209SBen Dooks /* no gpio support, empty definition for sm501_devdata. */
59f2999209SBen Dooks };
60f2999209SBen Dooks #endif
61f61be273SBen Dooks
62b6d6454fSBen Dooks struct sm501_devdata {
63b6d6454fSBen Dooks spinlock_t reg_lock;
64b6d6454fSBen Dooks struct mutex clock_lock;
65b6d6454fSBen Dooks struct list_head devices;
66f61be273SBen Dooks struct sm501_gpio gpio;
67b6d6454fSBen Dooks
68b6d6454fSBen Dooks struct device *dev;
69b6d6454fSBen Dooks struct resource *io_res;
70b6d6454fSBen Dooks struct resource *mem_res;
71b6d6454fSBen Dooks struct resource *regs_claim;
72b6d6454fSBen Dooks struct sm501_platdata *platdata;
73b6d6454fSBen Dooks
74f61be273SBen Dooks
75331d7475SBen Dooks unsigned int in_suspend;
76331d7475SBen Dooks unsigned long pm_misc;
77331d7475SBen Dooks
78b6d6454fSBen Dooks int unit_power[20];
79b6d6454fSBen Dooks unsigned int pdev_id;
80b6d6454fSBen Dooks unsigned int irq;
81b6d6454fSBen Dooks void __iomem *regs;
823149be50SVille Syrjala unsigned int rev;
83b6d6454fSBen Dooks };
84b6d6454fSBen Dooks
85f61be273SBen Dooks
86b6d6454fSBen Dooks #define MHZ (1000 * 1000)
87b6d6454fSBen Dooks
88b6d6454fSBen Dooks #ifdef DEBUG
89245904a4SVille Syrjala static const unsigned int div_tab[] = {
90b6d6454fSBen Dooks [0] = 1,
91b6d6454fSBen Dooks [1] = 2,
92b6d6454fSBen Dooks [2] = 4,
93b6d6454fSBen Dooks [3] = 8,
94b6d6454fSBen Dooks [4] = 16,
95b6d6454fSBen Dooks [5] = 32,
96b6d6454fSBen Dooks [6] = 64,
97b6d6454fSBen Dooks [7] = 128,
98b6d6454fSBen Dooks [8] = 3,
99b6d6454fSBen Dooks [9] = 6,
100b6d6454fSBen Dooks [10] = 12,
101b6d6454fSBen Dooks [11] = 24,
102b6d6454fSBen Dooks [12] = 48,
103b6d6454fSBen Dooks [13] = 96,
104b6d6454fSBen Dooks [14] = 192,
105b6d6454fSBen Dooks [15] = 384,
106b6d6454fSBen Dooks [16] = 5,
107b6d6454fSBen Dooks [17] = 10,
108b6d6454fSBen Dooks [18] = 20,
109b6d6454fSBen Dooks [19] = 40,
110b6d6454fSBen Dooks [20] = 80,
111b6d6454fSBen Dooks [21] = 160,
112b6d6454fSBen Dooks [22] = 320,
113b6d6454fSBen Dooks [23] = 604,
114b6d6454fSBen Dooks };
115b6d6454fSBen Dooks
decode_div(unsigned long pll2,unsigned long val,unsigned int lshft,unsigned int selbit,unsigned long mask)116b6d6454fSBen Dooks static unsigned long decode_div(unsigned long pll2, unsigned long val,
117b6d6454fSBen Dooks unsigned int lshft, unsigned int selbit,
118245904a4SVille Syrjala unsigned long mask)
119b6d6454fSBen Dooks {
120b6d6454fSBen Dooks if (val & selbit)
121b6d6454fSBen Dooks pll2 = 288 * MHZ;
122b6d6454fSBen Dooks
123245904a4SVille Syrjala return pll2 / div_tab[(val >> lshft) & mask];
124b6d6454fSBen Dooks }
125b6d6454fSBen Dooks
126b6d6454fSBen Dooks #define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
127b6d6454fSBen Dooks
128b6d6454fSBen Dooks /* sm501_dump_clk
129b6d6454fSBen Dooks *
130b6d6454fSBen Dooks * Print out the current clock configuration for the device
131b6d6454fSBen Dooks */
132b6d6454fSBen Dooks
sm501_dump_clk(struct sm501_devdata * sm)133b6d6454fSBen Dooks static void sm501_dump_clk(struct sm501_devdata *sm)
134b6d6454fSBen Dooks {
135bf5f0019SHeiko Schocher unsigned long misct = smc501_readl(sm->regs + SM501_MISC_TIMING);
136bf5f0019SHeiko Schocher unsigned long pm0 = smc501_readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
137bf5f0019SHeiko Schocher unsigned long pm1 = smc501_readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
138bf5f0019SHeiko Schocher unsigned long pmc = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
139b6d6454fSBen Dooks unsigned long sdclk0, sdclk1;
140b6d6454fSBen Dooks unsigned long pll2 = 0;
141b6d6454fSBen Dooks
142b6d6454fSBen Dooks switch (misct & 0x30) {
143b6d6454fSBen Dooks case 0x00:
144b6d6454fSBen Dooks pll2 = 336 * MHZ;
145b6d6454fSBen Dooks break;
146b6d6454fSBen Dooks case 0x10:
147b6d6454fSBen Dooks pll2 = 288 * MHZ;
148b6d6454fSBen Dooks break;
149b6d6454fSBen Dooks case 0x20:
150b6d6454fSBen Dooks pll2 = 240 * MHZ;
151b6d6454fSBen Dooks break;
152b6d6454fSBen Dooks case 0x30:
153b6d6454fSBen Dooks pll2 = 192 * MHZ;
154b6d6454fSBen Dooks break;
155b6d6454fSBen Dooks }
156b6d6454fSBen Dooks
157b6d6454fSBen Dooks sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
158245904a4SVille Syrjala sdclk0 /= div_tab[((misct >> 8) & 0xf)];
159b6d6454fSBen Dooks
160b6d6454fSBen Dooks sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
161245904a4SVille Syrjala sdclk1 /= div_tab[((misct >> 16) & 0xf)];
162b6d6454fSBen Dooks
163b6d6454fSBen Dooks dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
164b6d6454fSBen Dooks misct, pm0, pm1);
165b6d6454fSBen Dooks
166b6d6454fSBen Dooks dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
167b6d6454fSBen Dooks fmt_freq(pll2), sdclk0, sdclk1);
168b6d6454fSBen Dooks
169b6d6454fSBen Dooks dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
170b6d6454fSBen Dooks
171b6d6454fSBen Dooks dev_dbg(sm->dev, "PM0[%c]: "
172b6d6454fSBen Dooks "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
17348986f06SBen Dooks "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
174b6d6454fSBen Dooks (pmc & 3 ) == 0 ? '*' : '-',
175245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
176245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
177245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15)),
178245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15)));
179b6d6454fSBen Dooks
180b6d6454fSBen Dooks dev_dbg(sm->dev, "PM1[%c]: "
181b6d6454fSBen Dooks "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
182b6d6454fSBen Dooks "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
183b6d6454fSBen Dooks (pmc & 3 ) == 1 ? '*' : '-',
184245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
185245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
186245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15)),
187245904a4SVille Syrjala fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15)));
188b6d6454fSBen Dooks }
189331d7475SBen Dooks
sm501_dump_regs(struct sm501_devdata * sm)190331d7475SBen Dooks static void sm501_dump_regs(struct sm501_devdata *sm)
191b6d6454fSBen Dooks {
192331d7475SBen Dooks void __iomem *regs = sm->regs;
193331d7475SBen Dooks
194331d7475SBen Dooks dev_info(sm->dev, "System Control %08x\n",
195bf5f0019SHeiko Schocher smc501_readl(regs + SM501_SYSTEM_CONTROL));
196331d7475SBen Dooks dev_info(sm->dev, "Misc Control %08x\n",
197bf5f0019SHeiko Schocher smc501_readl(regs + SM501_MISC_CONTROL));
198331d7475SBen Dooks dev_info(sm->dev, "GPIO Control Low %08x\n",
199bf5f0019SHeiko Schocher smc501_readl(regs + SM501_GPIO31_0_CONTROL));
200331d7475SBen Dooks dev_info(sm->dev, "GPIO Control Hi %08x\n",
201bf5f0019SHeiko Schocher smc501_readl(regs + SM501_GPIO63_32_CONTROL));
202331d7475SBen Dooks dev_info(sm->dev, "DRAM Control %08x\n",
203bf5f0019SHeiko Schocher smc501_readl(regs + SM501_DRAM_CONTROL));
204331d7475SBen Dooks dev_info(sm->dev, "Arbitration Ctrl %08x\n",
205bf5f0019SHeiko Schocher smc501_readl(regs + SM501_ARBTRTN_CONTROL));
206331d7475SBen Dooks dev_info(sm->dev, "Misc Timing %08x\n",
207bf5f0019SHeiko Schocher smc501_readl(regs + SM501_MISC_TIMING));
208b6d6454fSBen Dooks }
209331d7475SBen Dooks
sm501_dump_gate(struct sm501_devdata * sm)210331d7475SBen Dooks static void sm501_dump_gate(struct sm501_devdata *sm)
211331d7475SBen Dooks {
212331d7475SBen Dooks dev_info(sm->dev, "CurrentGate %08x\n",
213bf5f0019SHeiko Schocher smc501_readl(sm->regs + SM501_CURRENT_GATE));
214331d7475SBen Dooks dev_info(sm->dev, "CurrentClock %08x\n",
215bf5f0019SHeiko Schocher smc501_readl(sm->regs + SM501_CURRENT_CLOCK));
216331d7475SBen Dooks dev_info(sm->dev, "PowerModeControl %08x\n",
217bf5f0019SHeiko Schocher smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL));
218331d7475SBen Dooks }
219331d7475SBen Dooks
220331d7475SBen Dooks #else
sm501_dump_gate(struct sm501_devdata * sm)221331d7475SBen Dooks static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
sm501_dump_regs(struct sm501_devdata * sm)222331d7475SBen Dooks static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
sm501_dump_clk(struct sm501_devdata * sm)223331d7475SBen Dooks static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
224b6d6454fSBen Dooks #endif
225b6d6454fSBen Dooks
226b6d6454fSBen Dooks /* sm501_sync_regs
227b6d6454fSBen Dooks *
228b6d6454fSBen Dooks * ensure the
229b6d6454fSBen Dooks */
230b6d6454fSBen Dooks
sm501_sync_regs(struct sm501_devdata * sm)231b6d6454fSBen Dooks static void sm501_sync_regs(struct sm501_devdata *sm)
232b6d6454fSBen Dooks {
233bf5f0019SHeiko Schocher smc501_readl(sm->regs);
234b6d6454fSBen Dooks }
235b6d6454fSBen Dooks
sm501_mdelay(struct sm501_devdata * sm,unsigned int delay)236331d7475SBen Dooks static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
237331d7475SBen Dooks {
238331d7475SBen Dooks /* during suspend/resume, we are currently not allowed to sleep,
239331d7475SBen Dooks * so change to using mdelay() instead of msleep() if we
240331d7475SBen Dooks * are in one of these paths */
241331d7475SBen Dooks
242331d7475SBen Dooks if (sm->in_suspend)
243331d7475SBen Dooks mdelay(delay);
244331d7475SBen Dooks else
245331d7475SBen Dooks msleep(delay);
246331d7475SBen Dooks }
247331d7475SBen Dooks
248b6d6454fSBen Dooks /* sm501_misc_control
249b6d6454fSBen Dooks *
250331d7475SBen Dooks * alters the miscellaneous control parameters
251b6d6454fSBen Dooks */
252b6d6454fSBen Dooks
sm501_misc_control(struct device * dev,unsigned long set,unsigned long clear)253b6d6454fSBen Dooks int sm501_misc_control(struct device *dev,
254b6d6454fSBen Dooks unsigned long set, unsigned long clear)
255b6d6454fSBen Dooks {
256b6d6454fSBen Dooks struct sm501_devdata *sm = dev_get_drvdata(dev);
257b6d6454fSBen Dooks unsigned long misc;
258b6d6454fSBen Dooks unsigned long save;
259b6d6454fSBen Dooks unsigned long to;
260b6d6454fSBen Dooks
261b6d6454fSBen Dooks spin_lock_irqsave(&sm->reg_lock, save);
262b6d6454fSBen Dooks
263bf5f0019SHeiko Schocher misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
264b6d6454fSBen Dooks to = (misc & ~clear) | set;
265b6d6454fSBen Dooks
266b6d6454fSBen Dooks if (to != misc) {
267bf5f0019SHeiko Schocher smc501_writel(to, sm->regs + SM501_MISC_CONTROL);
268b6d6454fSBen Dooks sm501_sync_regs(sm);
269b6d6454fSBen Dooks
270b6d6454fSBen Dooks dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
271b6d6454fSBen Dooks }
272b6d6454fSBen Dooks
273b6d6454fSBen Dooks spin_unlock_irqrestore(&sm->reg_lock, save);
274b6d6454fSBen Dooks return to;
275b6d6454fSBen Dooks }
276b6d6454fSBen Dooks
277b6d6454fSBen Dooks EXPORT_SYMBOL_GPL(sm501_misc_control);
278b6d6454fSBen Dooks
279b6d6454fSBen Dooks /* sm501_modify_reg
280b6d6454fSBen Dooks *
281b6d6454fSBen Dooks * Modify a register in the SM501 which may be shared with other
282b6d6454fSBen Dooks * drivers.
283b6d6454fSBen Dooks */
284b6d6454fSBen Dooks
sm501_modify_reg(struct device * dev,unsigned long reg,unsigned long set,unsigned long clear)285b6d6454fSBen Dooks unsigned long sm501_modify_reg(struct device *dev,
286b6d6454fSBen Dooks unsigned long reg,
287b6d6454fSBen Dooks unsigned long set,
288b6d6454fSBen Dooks unsigned long clear)
289b6d6454fSBen Dooks {
290b6d6454fSBen Dooks struct sm501_devdata *sm = dev_get_drvdata(dev);
291b6d6454fSBen Dooks unsigned long data;
292b6d6454fSBen Dooks unsigned long save;
293b6d6454fSBen Dooks
294b6d6454fSBen Dooks spin_lock_irqsave(&sm->reg_lock, save);
295b6d6454fSBen Dooks
296bf5f0019SHeiko Schocher data = smc501_readl(sm->regs + reg);
297b6d6454fSBen Dooks data |= set;
298b6d6454fSBen Dooks data &= ~clear;
299b6d6454fSBen Dooks
300bf5f0019SHeiko Schocher smc501_writel(data, sm->regs + reg);
301b6d6454fSBen Dooks sm501_sync_regs(sm);
302b6d6454fSBen Dooks
303b6d6454fSBen Dooks spin_unlock_irqrestore(&sm->reg_lock, save);
304b6d6454fSBen Dooks
305b6d6454fSBen Dooks return data;
306b6d6454fSBen Dooks }
307b6d6454fSBen Dooks
308b6d6454fSBen Dooks EXPORT_SYMBOL_GPL(sm501_modify_reg);
309b6d6454fSBen Dooks
310b6d6454fSBen Dooks /* sm501_unit_power
311b6d6454fSBen Dooks *
312b6d6454fSBen Dooks * alters the power active gate to set specific units on or off
313b6d6454fSBen Dooks */
314b6d6454fSBen Dooks
sm501_unit_power(struct device * dev,unsigned int unit,unsigned int to)315b6d6454fSBen Dooks int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
316b6d6454fSBen Dooks {
317b6d6454fSBen Dooks struct sm501_devdata *sm = dev_get_drvdata(dev);
318b6d6454fSBen Dooks unsigned long mode;
319b6d6454fSBen Dooks unsigned long gate;
320b6d6454fSBen Dooks unsigned long clock;
321b6d6454fSBen Dooks
322b6d6454fSBen Dooks mutex_lock(&sm->clock_lock);
323b6d6454fSBen Dooks
324bf5f0019SHeiko Schocher mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
325bf5f0019SHeiko Schocher gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
326bf5f0019SHeiko Schocher clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
327b6d6454fSBen Dooks
328b6d6454fSBen Dooks mode &= 3; /* get current power mode */
329b6d6454fSBen Dooks
330bf703c3fSAdrian Bunk if (unit >= ARRAY_SIZE(sm->unit_power)) {
331145980a0SHarvey Harrison dev_err(dev, "%s: bad unit %d\n", __func__, unit);
332b6d6454fSBen Dooks goto already;
333b6d6454fSBen Dooks }
334b6d6454fSBen Dooks
335145980a0SHarvey Harrison dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __func__, unit,
336b6d6454fSBen Dooks sm->unit_power[unit], to);
337b6d6454fSBen Dooks
338b6d6454fSBen Dooks if (to == 0 && sm->unit_power[unit] == 0) {
339b6d6454fSBen Dooks dev_err(sm->dev, "unit %d is already shutdown\n", unit);
340b6d6454fSBen Dooks goto already;
341b6d6454fSBen Dooks }
342b6d6454fSBen Dooks
343b6d6454fSBen Dooks sm->unit_power[unit] += to ? 1 : -1;
344b6d6454fSBen Dooks to = sm->unit_power[unit] ? 1 : 0;
345b6d6454fSBen Dooks
346b6d6454fSBen Dooks if (to) {
347b6d6454fSBen Dooks if (gate & (1 << unit))
348b6d6454fSBen Dooks goto already;
349b6d6454fSBen Dooks gate |= (1 << unit);
350b6d6454fSBen Dooks } else {
351b6d6454fSBen Dooks if (!(gate & (1 << unit)))
352b6d6454fSBen Dooks goto already;
353b6d6454fSBen Dooks gate &= ~(1 << unit);
354b6d6454fSBen Dooks }
355b6d6454fSBen Dooks
356b6d6454fSBen Dooks switch (mode) {
357b6d6454fSBen Dooks case 1:
358bf5f0019SHeiko Schocher smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
359bf5f0019SHeiko Schocher smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
360b6d6454fSBen Dooks mode = 0;
361b6d6454fSBen Dooks break;
362b6d6454fSBen Dooks case 2:
363b6d6454fSBen Dooks case 0:
364bf5f0019SHeiko Schocher smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
365bf5f0019SHeiko Schocher smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
366b6d6454fSBen Dooks mode = 1;
367b6d6454fSBen Dooks break;
368b6d6454fSBen Dooks
369b6d6454fSBen Dooks default:
370992bb253SJiri Slaby gate = -1;
371992bb253SJiri Slaby goto already;
372b6d6454fSBen Dooks }
373b6d6454fSBen Dooks
374bf5f0019SHeiko Schocher smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
375b6d6454fSBen Dooks sm501_sync_regs(sm);
376b6d6454fSBen Dooks
377b6d6454fSBen Dooks dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
378b6d6454fSBen Dooks gate, clock, mode);
379b6d6454fSBen Dooks
380331d7475SBen Dooks sm501_mdelay(sm, 16);
381b6d6454fSBen Dooks
382b6d6454fSBen Dooks already:
383b6d6454fSBen Dooks mutex_unlock(&sm->clock_lock);
384b6d6454fSBen Dooks return gate;
385b6d6454fSBen Dooks }
386b6d6454fSBen Dooks
387b6d6454fSBen Dooks EXPORT_SYMBOL_GPL(sm501_unit_power);
388b6d6454fSBen Dooks
389b6d6454fSBen Dooks /* clock value structure. */
390b6d6454fSBen Dooks struct sm501_clock {
391b6d6454fSBen Dooks unsigned long mclk;
392b6d6454fSBen Dooks int divider;
393b6d6454fSBen Dooks int shift;
3943149be50SVille Syrjala unsigned int m, n, k;
395b6d6454fSBen Dooks };
396b6d6454fSBen Dooks
3973149be50SVille Syrjala /* sm501_calc_clock
398b6d6454fSBen Dooks *
3993149be50SVille Syrjala * Calculates the nearest discrete clock frequency that
4003149be50SVille Syrjala * can be achieved with the specified input clock.
401b6d6454fSBen Dooks * the maximum divisor is 3 or 5
402b6d6454fSBen Dooks */
4033149be50SVille Syrjala
sm501_calc_clock(unsigned long freq,struct sm501_clock * clock,int max_div,unsigned long mclk,long * best_diff)4043149be50SVille Syrjala static int sm501_calc_clock(unsigned long freq,
405b6d6454fSBen Dooks struct sm501_clock *clock,
4063149be50SVille Syrjala int max_div,
4073149be50SVille Syrjala unsigned long mclk,
4083149be50SVille Syrjala long *best_diff)
409b6d6454fSBen Dooks {
4103149be50SVille Syrjala int ret = 0;
411b6d6454fSBen Dooks int divider;
412b6d6454fSBen Dooks int shift;
413b6d6454fSBen Dooks long diff;
414b6d6454fSBen Dooks
415b6d6454fSBen Dooks /* try dividers 1 and 3 for CRT and for panel,
416b6d6454fSBen Dooks try divider 5 for panel only.*/
417b6d6454fSBen Dooks
418b6d6454fSBen Dooks for (divider = 1; divider <= max_div; divider += 2) {
419b6d6454fSBen Dooks /* try all 8 shift values.*/
420b6d6454fSBen Dooks for (shift = 0; shift < 8; shift++) {
421b6d6454fSBen Dooks /* Calculate difference to requested clock */
422829ecbcbSAxel Lin diff = DIV_ROUND_CLOSEST(mclk, divider << shift) - freq;
423b6d6454fSBen Dooks if (diff < 0)
424b6d6454fSBen Dooks diff = -diff;
425b6d6454fSBen Dooks
426b6d6454fSBen Dooks /* If it is less than the current, use it */
4273149be50SVille Syrjala if (diff < *best_diff) {
4283149be50SVille Syrjala *best_diff = diff;
429b6d6454fSBen Dooks
430b6d6454fSBen Dooks clock->mclk = mclk;
431b6d6454fSBen Dooks clock->divider = divider;
432b6d6454fSBen Dooks clock->shift = shift;
4333149be50SVille Syrjala ret = 1;
434b6d6454fSBen Dooks }
435b6d6454fSBen Dooks }
436b6d6454fSBen Dooks }
4373149be50SVille Syrjala
4383149be50SVille Syrjala return ret;
4393149be50SVille Syrjala }
4403149be50SVille Syrjala
4413149be50SVille Syrjala /* sm501_calc_pll
4423149be50SVille Syrjala *
4433149be50SVille Syrjala * Calculates the nearest discrete clock frequency that can be
4443149be50SVille Syrjala * achieved using the programmable PLL.
4453149be50SVille Syrjala * the maximum divisor is 3 or 5
4463149be50SVille Syrjala */
4473149be50SVille Syrjala
sm501_calc_pll(unsigned long freq,struct sm501_clock * clock,int max_div)4483149be50SVille Syrjala static unsigned long sm501_calc_pll(unsigned long freq,
4493149be50SVille Syrjala struct sm501_clock *clock,
4503149be50SVille Syrjala int max_div)
4513149be50SVille Syrjala {
4523149be50SVille Syrjala unsigned long mclk;
4533149be50SVille Syrjala unsigned int m, n, k;
4543149be50SVille Syrjala long best_diff = 999999999;
4553149be50SVille Syrjala
4563149be50SVille Syrjala /*
4573149be50SVille Syrjala * The SM502 datasheet doesn't specify the min/max values for M and N.
4583149be50SVille Syrjala * N = 1 at least doesn't work in practice.
4593149be50SVille Syrjala */
4603149be50SVille Syrjala for (m = 2; m <= 255; m++) {
4613149be50SVille Syrjala for (n = 2; n <= 127; n++) {
4623149be50SVille Syrjala for (k = 0; k <= 1; k++) {
4633149be50SVille Syrjala mclk = (24000000UL * m / n) >> k;
4643149be50SVille Syrjala
4653149be50SVille Syrjala if (sm501_calc_clock(freq, clock, max_div,
4663149be50SVille Syrjala mclk, &best_diff)) {
4673149be50SVille Syrjala clock->m = m;
4683149be50SVille Syrjala clock->n = n;
4693149be50SVille Syrjala clock->k = k;
4703149be50SVille Syrjala }
4713149be50SVille Syrjala }
4723149be50SVille Syrjala }
4733149be50SVille Syrjala }
4743149be50SVille Syrjala
4753149be50SVille Syrjala /* Return best clock. */
4763149be50SVille Syrjala return clock->mclk / (clock->divider << clock->shift);
4773149be50SVille Syrjala }
4783149be50SVille Syrjala
4793149be50SVille Syrjala /* sm501_select_clock
4803149be50SVille Syrjala *
4813149be50SVille Syrjala * Calculates the nearest discrete clock frequency that can be
4823149be50SVille Syrjala * achieved using the 288MHz and 336MHz PLLs.
4833149be50SVille Syrjala * the maximum divisor is 3 or 5
4843149be50SVille Syrjala */
4853149be50SVille Syrjala
sm501_select_clock(unsigned long freq,struct sm501_clock * clock,int max_div)4863149be50SVille Syrjala static unsigned long sm501_select_clock(unsigned long freq,
4873149be50SVille Syrjala struct sm501_clock *clock,
4883149be50SVille Syrjala int max_div)
4893149be50SVille Syrjala {
4903149be50SVille Syrjala unsigned long mclk;
4913149be50SVille Syrjala long best_diff = 999999999;
4923149be50SVille Syrjala
4933149be50SVille Syrjala /* Try 288MHz and 336MHz clocks. */
4943149be50SVille Syrjala for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
4953149be50SVille Syrjala sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
496b6d6454fSBen Dooks }
497b6d6454fSBen Dooks
498b6d6454fSBen Dooks /* Return best clock. */
499b6d6454fSBen Dooks return clock->mclk / (clock->divider << clock->shift);
500b6d6454fSBen Dooks }
501b6d6454fSBen Dooks
502b6d6454fSBen Dooks /* sm501_set_clock
503b6d6454fSBen Dooks *
504b6d6454fSBen Dooks * set one of the four clock sources to the closest available frequency to
505b6d6454fSBen Dooks * the one specified
506b6d6454fSBen Dooks */
507b6d6454fSBen Dooks
sm501_set_clock(struct device * dev,int clksrc,unsigned long req_freq)508b6d6454fSBen Dooks unsigned long sm501_set_clock(struct device *dev,
509b6d6454fSBen Dooks int clksrc,
510b6d6454fSBen Dooks unsigned long req_freq)
511b6d6454fSBen Dooks {
512b6d6454fSBen Dooks struct sm501_devdata *sm = dev_get_drvdata(dev);
513bf5f0019SHeiko Schocher unsigned long mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
514bf5f0019SHeiko Schocher unsigned long gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
515bf5f0019SHeiko Schocher unsigned long clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
5163149be50SVille Syrjala unsigned int pll_reg = 0;
5173ad2f3fbSDaniel Mack unsigned long sm501_freq; /* the actual frequency achieved */
5185f114ebcSLee Jones u64 reg;
519b6d6454fSBen Dooks
520b6d6454fSBen Dooks struct sm501_clock to;
521b6d6454fSBen Dooks
522b6d6454fSBen Dooks /* find achivable discrete frequency and setup register value
523b6d6454fSBen Dooks * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
524b6d6454fSBen Dooks * has an extra bit for the divider */
525b6d6454fSBen Dooks
526b6d6454fSBen Dooks switch (clksrc) {
527b6d6454fSBen Dooks case SM501_CLOCK_P2XCLK:
5283ad2f3fbSDaniel Mack /* This clock is divided in half so to achieve the
529b6d6454fSBen Dooks * requested frequency the value must be multiplied by
530b6d6454fSBen Dooks * 2. This clock also has an additional pre divisor */
531b6d6454fSBen Dooks
5323149be50SVille Syrjala if (sm->rev >= 0xC0) {
5333149be50SVille Syrjala /* SM502 -> use the programmable PLL */
5343149be50SVille Syrjala sm501_freq = (sm501_calc_pll(2 * req_freq,
5353149be50SVille Syrjala &to, 5) / 2);
5363149be50SVille Syrjala reg = to.shift & 0x07;/* bottom 3 bits are shift */
5373149be50SVille Syrjala if (to.divider == 3)
5383149be50SVille Syrjala reg |= 0x08; /* /3 divider required */
5393149be50SVille Syrjala else if (to.divider == 5)
5403149be50SVille Syrjala reg |= 0x10; /* /5 divider required */
5413149be50SVille Syrjala reg |= 0x40; /* select the programmable PLL */
5423149be50SVille Syrjala pll_reg = 0x20000 | (to.k << 15) | (to.n << 8) | to.m;
5433149be50SVille Syrjala } else {
5443149be50SVille Syrjala sm501_freq = (sm501_select_clock(2 * req_freq,
5453149be50SVille Syrjala &to, 5) / 2);
546b6d6454fSBen Dooks reg = to.shift & 0x07;/* bottom 3 bits are shift */
547b6d6454fSBen Dooks if (to.divider == 3)
548b6d6454fSBen Dooks reg |= 0x08; /* /3 divider required */
549b6d6454fSBen Dooks else if (to.divider == 5)
550b6d6454fSBen Dooks reg |= 0x10; /* /5 divider required */
551b6d6454fSBen Dooks if (to.mclk != 288000000)
552b6d6454fSBen Dooks reg |= 0x20; /* which mclk pll is source */
5533149be50SVille Syrjala }
554b6d6454fSBen Dooks break;
555b6d6454fSBen Dooks
556b6d6454fSBen Dooks case SM501_CLOCK_V2XCLK:
5573ad2f3fbSDaniel Mack /* This clock is divided in half so to achieve the
558b6d6454fSBen Dooks * requested frequency the value must be multiplied by 2. */
559b6d6454fSBen Dooks
560b6d6454fSBen Dooks sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
561b6d6454fSBen Dooks reg=to.shift & 0x07; /* bottom 3 bits are shift */
562b6d6454fSBen Dooks if (to.divider == 3)
563b6d6454fSBen Dooks reg |= 0x08; /* /3 divider required */
564b6d6454fSBen Dooks if (to.mclk != 288000000)
565b6d6454fSBen Dooks reg |= 0x10; /* which mclk pll is source */
566b6d6454fSBen Dooks break;
567b6d6454fSBen Dooks
568b6d6454fSBen Dooks case SM501_CLOCK_MCLK:
569b6d6454fSBen Dooks case SM501_CLOCK_M1XCLK:
570b6d6454fSBen Dooks /* These clocks are the same and not further divided */
571b6d6454fSBen Dooks
572b6d6454fSBen Dooks sm501_freq = sm501_select_clock( req_freq, &to, 3);
573b6d6454fSBen Dooks reg=to.shift & 0x07; /* bottom 3 bits are shift */
574b6d6454fSBen Dooks if (to.divider == 3)
575b6d6454fSBen Dooks reg |= 0x08; /* /3 divider required */
576b6d6454fSBen Dooks if (to.mclk != 288000000)
577b6d6454fSBen Dooks reg |= 0x10; /* which mclk pll is source */
578b6d6454fSBen Dooks break;
579b6d6454fSBen Dooks
580b6d6454fSBen Dooks default:
581b6d6454fSBen Dooks return 0; /* this is bad */
582b6d6454fSBen Dooks }
583b6d6454fSBen Dooks
584b6d6454fSBen Dooks mutex_lock(&sm->clock_lock);
585b6d6454fSBen Dooks
586bf5f0019SHeiko Schocher mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
587bf5f0019SHeiko Schocher gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
588bf5f0019SHeiko Schocher clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
589b6d6454fSBen Dooks
590b6d6454fSBen Dooks clock = clock & ~(0xFF << clksrc);
591b6d6454fSBen Dooks clock |= reg<<clksrc;
592b6d6454fSBen Dooks
593b6d6454fSBen Dooks mode &= 3; /* find current mode */
594b6d6454fSBen Dooks
595b6d6454fSBen Dooks switch (mode) {
596b6d6454fSBen Dooks case 1:
597bf5f0019SHeiko Schocher smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
598bf5f0019SHeiko Schocher smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
599b6d6454fSBen Dooks mode = 0;
600b6d6454fSBen Dooks break;
601b6d6454fSBen Dooks case 2:
602b6d6454fSBen Dooks case 0:
603bf5f0019SHeiko Schocher smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
604bf5f0019SHeiko Schocher smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
605b6d6454fSBen Dooks mode = 1;
606b6d6454fSBen Dooks break;
607b6d6454fSBen Dooks
608b6d6454fSBen Dooks default:
609b6d6454fSBen Dooks mutex_unlock(&sm->clock_lock);
610b6d6454fSBen Dooks return -1;
611b6d6454fSBen Dooks }
612b6d6454fSBen Dooks
613bf5f0019SHeiko Schocher smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
6143149be50SVille Syrjala
6153149be50SVille Syrjala if (pll_reg)
616bf5f0019SHeiko Schocher smc501_writel(pll_reg,
617bf5f0019SHeiko Schocher sm->regs + SM501_PROGRAMMABLE_PLL_CONTROL);
6183149be50SVille Syrjala
619b6d6454fSBen Dooks sm501_sync_regs(sm);
620b6d6454fSBen Dooks
62180e74a80SBen Dooks dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
622b6d6454fSBen Dooks gate, clock, mode);
623b6d6454fSBen Dooks
624331d7475SBen Dooks sm501_mdelay(sm, 16);
625b6d6454fSBen Dooks mutex_unlock(&sm->clock_lock);
626b6d6454fSBen Dooks
627b6d6454fSBen Dooks sm501_dump_clk(sm);
628b6d6454fSBen Dooks
629b6d6454fSBen Dooks return sm501_freq;
630b6d6454fSBen Dooks }
631b6d6454fSBen Dooks
632b6d6454fSBen Dooks EXPORT_SYMBOL_GPL(sm501_set_clock);
633b6d6454fSBen Dooks
634b6d6454fSBen Dooks /* sm501_find_clock
635b6d6454fSBen Dooks *
636b6d6454fSBen Dooks * finds the closest available frequency for a given clock
637b6d6454fSBen Dooks */
638b6d6454fSBen Dooks
sm501_find_clock(struct device * dev,int clksrc,unsigned long req_freq)6393149be50SVille Syrjala unsigned long sm501_find_clock(struct device *dev,
6403149be50SVille Syrjala int clksrc,
641b6d6454fSBen Dooks unsigned long req_freq)
642b6d6454fSBen Dooks {
6433149be50SVille Syrjala struct sm501_devdata *sm = dev_get_drvdata(dev);
6443ad2f3fbSDaniel Mack unsigned long sm501_freq; /* the frequency achieveable by the 501 */
645b6d6454fSBen Dooks struct sm501_clock to;
646b6d6454fSBen Dooks
647b6d6454fSBen Dooks switch (clksrc) {
648b6d6454fSBen Dooks case SM501_CLOCK_P2XCLK:
6493149be50SVille Syrjala if (sm->rev >= 0xC0) {
6503149be50SVille Syrjala /* SM502 -> use the programmable PLL */
6513149be50SVille Syrjala sm501_freq = (sm501_calc_pll(2 * req_freq,
6523149be50SVille Syrjala &to, 5) / 2);
6533149be50SVille Syrjala } else {
6543149be50SVille Syrjala sm501_freq = (sm501_select_clock(2 * req_freq,
6553149be50SVille Syrjala &to, 5) / 2);
6563149be50SVille Syrjala }
657b6d6454fSBen Dooks break;
658b6d6454fSBen Dooks
659b6d6454fSBen Dooks case SM501_CLOCK_V2XCLK:
660b6d6454fSBen Dooks sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
661b6d6454fSBen Dooks break;
662b6d6454fSBen Dooks
663b6d6454fSBen Dooks case SM501_CLOCK_MCLK:
664b6d6454fSBen Dooks case SM501_CLOCK_M1XCLK:
665b6d6454fSBen Dooks sm501_freq = sm501_select_clock(req_freq, &to, 3);
666b6d6454fSBen Dooks break;
667b6d6454fSBen Dooks
668b6d6454fSBen Dooks default:
669b6d6454fSBen Dooks sm501_freq = 0; /* error */
670b6d6454fSBen Dooks }
671b6d6454fSBen Dooks
672b6d6454fSBen Dooks return sm501_freq;
673b6d6454fSBen Dooks }
674b6d6454fSBen Dooks
675b6d6454fSBen Dooks EXPORT_SYMBOL_GPL(sm501_find_clock);
676b6d6454fSBen Dooks
to_sm_device(struct platform_device * pdev)677b6d6454fSBen Dooks static struct sm501_device *to_sm_device(struct platform_device *pdev)
678b6d6454fSBen Dooks {
679b6d6454fSBen Dooks return container_of(pdev, struct sm501_device, pdev);
680b6d6454fSBen Dooks }
681b6d6454fSBen Dooks
682b6d6454fSBen Dooks /* sm501_device_release
683b6d6454fSBen Dooks *
684b6d6454fSBen Dooks * A release function for the platform devices we create to allow us to
685b6d6454fSBen Dooks * free any items we allocated
686b6d6454fSBen Dooks */
687b6d6454fSBen Dooks
sm501_device_release(struct device * dev)688b6d6454fSBen Dooks static void sm501_device_release(struct device *dev)
689b6d6454fSBen Dooks {
690b6d6454fSBen Dooks kfree(to_sm_device(to_platform_device(dev)));
691b6d6454fSBen Dooks }
692b6d6454fSBen Dooks
693b6d6454fSBen Dooks /* sm501_create_subdev
694b6d6454fSBen Dooks *
695b6d6454fSBen Dooks * Create a skeleton platform device with resources for passing to a
696b6d6454fSBen Dooks * sub-driver
697b6d6454fSBen Dooks */
698b6d6454fSBen Dooks
699b6d6454fSBen Dooks static struct platform_device *
sm501_create_subdev(struct sm501_devdata * sm,char * name,unsigned int res_count,unsigned int platform_data_size)70061711f8fSMagnus Damm sm501_create_subdev(struct sm501_devdata *sm, char *name,
70161711f8fSMagnus Damm unsigned int res_count, unsigned int platform_data_size)
702b6d6454fSBen Dooks {
703b6d6454fSBen Dooks struct sm501_device *smdev;
704b6d6454fSBen Dooks
705b6d6454fSBen Dooks smdev = kzalloc(sizeof(struct sm501_device) +
70661711f8fSMagnus Damm (sizeof(struct resource) * res_count) +
70761711f8fSMagnus Damm platform_data_size, GFP_KERNEL);
708b6d6454fSBen Dooks if (!smdev)
709b6d6454fSBen Dooks return NULL;
710b6d6454fSBen Dooks
711b6d6454fSBen Dooks smdev->pdev.dev.release = sm501_device_release;
712b6d6454fSBen Dooks
713b6d6454fSBen Dooks smdev->pdev.name = name;
714b6d6454fSBen Dooks smdev->pdev.id = sm->pdev_id;
71561711f8fSMagnus Damm smdev->pdev.dev.parent = sm->dev;
7162f606da7SGuenter Roeck smdev->pdev.dev.coherent_dma_mask = 0xffffffff;
71761711f8fSMagnus Damm
71861711f8fSMagnus Damm if (res_count) {
719b6d6454fSBen Dooks smdev->pdev.resource = (struct resource *)(smdev+1);
720b6d6454fSBen Dooks smdev->pdev.num_resources = res_count;
72161711f8fSMagnus Damm }
72261711f8fSMagnus Damm if (platform_data_size)
72361711f8fSMagnus Damm smdev->pdev.dev.platform_data = (void *)(smdev+1);
724b6d6454fSBen Dooks
725b6d6454fSBen Dooks return &smdev->pdev;
726b6d6454fSBen Dooks }
727b6d6454fSBen Dooks
728b6d6454fSBen Dooks /* sm501_register_device
729b6d6454fSBen Dooks *
730b6d6454fSBen Dooks * Register a platform device created with sm501_create_subdev()
731b6d6454fSBen Dooks */
732b6d6454fSBen Dooks
sm501_register_device(struct sm501_devdata * sm,struct platform_device * pdev)733b6d6454fSBen Dooks static int sm501_register_device(struct sm501_devdata *sm,
734b6d6454fSBen Dooks struct platform_device *pdev)
735b6d6454fSBen Dooks {
736b6d6454fSBen Dooks struct sm501_device *smdev = to_sm_device(pdev);
737b6d6454fSBen Dooks int ptr;
738b6d6454fSBen Dooks int ret;
739b6d6454fSBen Dooks
740b6d6454fSBen Dooks for (ptr = 0; ptr < pdev->num_resources; ptr++) {
7413f3d4310SJoe Perches printk(KERN_DEBUG "%s[%d] %pR\n",
7423f3d4310SJoe Perches pdev->name, ptr, &pdev->resource[ptr]);
743b6d6454fSBen Dooks }
744b6d6454fSBen Dooks
745b6d6454fSBen Dooks ret = platform_device_register(pdev);
746b6d6454fSBen Dooks
747b6d6454fSBen Dooks if (ret >= 0) {
748b6d6454fSBen Dooks dev_dbg(sm->dev, "registered %s\n", pdev->name);
749b6d6454fSBen Dooks list_add_tail(&smdev->list, &sm->devices);
750b6d6454fSBen Dooks } else
751b6d6454fSBen Dooks dev_err(sm->dev, "error registering %s (%d)\n",
752b6d6454fSBen Dooks pdev->name, ret);
753b6d6454fSBen Dooks
754b6d6454fSBen Dooks return ret;
755b6d6454fSBen Dooks }
756b6d6454fSBen Dooks
757b6d6454fSBen Dooks /* sm501_create_subio
758b6d6454fSBen Dooks *
759b6d6454fSBen Dooks * Fill in an IO resource for a sub device
760b6d6454fSBen Dooks */
761b6d6454fSBen Dooks
sm501_create_subio(struct sm501_devdata * sm,struct resource * res,resource_size_t offs,resource_size_t size)762b6d6454fSBen Dooks static void sm501_create_subio(struct sm501_devdata *sm,
763b6d6454fSBen Dooks struct resource *res,
764b6d6454fSBen Dooks resource_size_t offs,
765b6d6454fSBen Dooks resource_size_t size)
766b6d6454fSBen Dooks {
767b6d6454fSBen Dooks res->flags = IORESOURCE_MEM;
768b6d6454fSBen Dooks res->parent = sm->io_res;
769b6d6454fSBen Dooks res->start = sm->io_res->start + offs;
770b6d6454fSBen Dooks res->end = res->start + size - 1;
771b6d6454fSBen Dooks }
772b6d6454fSBen Dooks
773b6d6454fSBen Dooks /* sm501_create_mem
774b6d6454fSBen Dooks *
775b6d6454fSBen Dooks * Fill in an MEM resource for a sub device
776b6d6454fSBen Dooks */
777b6d6454fSBen Dooks
sm501_create_mem(struct sm501_devdata * sm,struct resource * res,resource_size_t * offs,resource_size_t size)778b6d6454fSBen Dooks static void sm501_create_mem(struct sm501_devdata *sm,
779b6d6454fSBen Dooks struct resource *res,
780b6d6454fSBen Dooks resource_size_t *offs,
781b6d6454fSBen Dooks resource_size_t size)
782b6d6454fSBen Dooks {
783b6d6454fSBen Dooks *offs -= size; /* adjust memory size */
784b6d6454fSBen Dooks
785b6d6454fSBen Dooks res->flags = IORESOURCE_MEM;
786b6d6454fSBen Dooks res->parent = sm->mem_res;
787b6d6454fSBen Dooks res->start = sm->mem_res->start + *offs;
788b6d6454fSBen Dooks res->end = res->start + size - 1;
789b6d6454fSBen Dooks }
790b6d6454fSBen Dooks
791b6d6454fSBen Dooks /* sm501_create_irq
792b6d6454fSBen Dooks *
793b6d6454fSBen Dooks * Fill in an IRQ resource for a sub device
794b6d6454fSBen Dooks */
795b6d6454fSBen Dooks
sm501_create_irq(struct sm501_devdata * sm,struct resource * res)796b6d6454fSBen Dooks static void sm501_create_irq(struct sm501_devdata *sm,
797b6d6454fSBen Dooks struct resource *res)
798b6d6454fSBen Dooks {
799b6d6454fSBen Dooks res->flags = IORESOURCE_IRQ;
800b6d6454fSBen Dooks res->parent = NULL;
801b6d6454fSBen Dooks res->start = res->end = sm->irq;
802b6d6454fSBen Dooks }
803b6d6454fSBen Dooks
sm501_register_usbhost(struct sm501_devdata * sm,resource_size_t * mem_avail)804b6d6454fSBen Dooks static int sm501_register_usbhost(struct sm501_devdata *sm,
805b6d6454fSBen Dooks resource_size_t *mem_avail)
806b6d6454fSBen Dooks {
807b6d6454fSBen Dooks struct platform_device *pdev;
808b6d6454fSBen Dooks
80961711f8fSMagnus Damm pdev = sm501_create_subdev(sm, "sm501-usb", 3, 0);
810b6d6454fSBen Dooks if (!pdev)
811b6d6454fSBen Dooks return -ENOMEM;
812b6d6454fSBen Dooks
813b6d6454fSBen Dooks sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
814b6d6454fSBen Dooks sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
815b6d6454fSBen Dooks sm501_create_irq(sm, &pdev->resource[2]);
816b6d6454fSBen Dooks
817b6d6454fSBen Dooks return sm501_register_device(sm, pdev);
818b6d6454fSBen Dooks }
819b6d6454fSBen Dooks
sm501_setup_uart_data(struct sm501_devdata * sm,struct plat_serial8250_port * uart_data,unsigned int offset)82061711f8fSMagnus Damm static void sm501_setup_uart_data(struct sm501_devdata *sm,
82161711f8fSMagnus Damm struct plat_serial8250_port *uart_data,
82261711f8fSMagnus Damm unsigned int offset)
82361711f8fSMagnus Damm {
82461711f8fSMagnus Damm uart_data->membase = sm->regs + offset;
82561711f8fSMagnus Damm uart_data->mapbase = sm->io_res->start + offset;
82661711f8fSMagnus Damm uart_data->iotype = UPIO_MEM;
82761711f8fSMagnus Damm uart_data->irq = sm->irq;
82861711f8fSMagnus Damm uart_data->flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
82961711f8fSMagnus Damm uart_data->regshift = 2;
83061711f8fSMagnus Damm uart_data->uartclk = (9600 * 16);
83161711f8fSMagnus Damm }
83261711f8fSMagnus Damm
sm501_register_uart(struct sm501_devdata * sm,int devices)83361711f8fSMagnus Damm static int sm501_register_uart(struct sm501_devdata *sm, int devices)
83461711f8fSMagnus Damm {
83561711f8fSMagnus Damm struct platform_device *pdev;
83661711f8fSMagnus Damm struct plat_serial8250_port *uart_data;
83761711f8fSMagnus Damm
83861711f8fSMagnus Damm pdev = sm501_create_subdev(sm, "serial8250", 0,
83961711f8fSMagnus Damm sizeof(struct plat_serial8250_port) * 3);
84061711f8fSMagnus Damm if (!pdev)
84161711f8fSMagnus Damm return -ENOMEM;
84261711f8fSMagnus Damm
843334a41ceSJingoo Han uart_data = dev_get_platdata(&pdev->dev);
84461711f8fSMagnus Damm
84561711f8fSMagnus Damm if (devices & SM501_USE_UART0) {
84661711f8fSMagnus Damm sm501_setup_uart_data(sm, uart_data++, 0x30000);
84761711f8fSMagnus Damm sm501_unit_power(sm->dev, SM501_GATE_UART0, 1);
84861711f8fSMagnus Damm sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 12, 0);
84961711f8fSMagnus Damm sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x01e0, 0);
85061711f8fSMagnus Damm }
85161711f8fSMagnus Damm if (devices & SM501_USE_UART1) {
85261711f8fSMagnus Damm sm501_setup_uart_data(sm, uart_data++, 0x30020);
85361711f8fSMagnus Damm sm501_unit_power(sm->dev, SM501_GATE_UART1, 1);
85461711f8fSMagnus Damm sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 13, 0);
85561711f8fSMagnus Damm sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x1e00, 0);
85661711f8fSMagnus Damm }
85761711f8fSMagnus Damm
85861711f8fSMagnus Damm pdev->id = PLAT8250_DEV_SM501;
85961711f8fSMagnus Damm
86061711f8fSMagnus Damm return sm501_register_device(sm, pdev);
86161711f8fSMagnus Damm }
86261711f8fSMagnus Damm
sm501_register_display(struct sm501_devdata * sm,resource_size_t * mem_avail)863b6d6454fSBen Dooks static int sm501_register_display(struct sm501_devdata *sm,
864b6d6454fSBen Dooks resource_size_t *mem_avail)
865b6d6454fSBen Dooks {
866b6d6454fSBen Dooks struct platform_device *pdev;
867b6d6454fSBen Dooks
86861711f8fSMagnus Damm pdev = sm501_create_subdev(sm, "sm501-fb", 4, 0);
869b6d6454fSBen Dooks if (!pdev)
870b6d6454fSBen Dooks return -ENOMEM;
871b6d6454fSBen Dooks
872b6d6454fSBen Dooks sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
873b6d6454fSBen Dooks sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
874b6d6454fSBen Dooks sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
875b6d6454fSBen Dooks sm501_create_irq(sm, &pdev->resource[3]);
876b6d6454fSBen Dooks
877b6d6454fSBen Dooks return sm501_register_device(sm, pdev);
878b6d6454fSBen Dooks }
879b6d6454fSBen Dooks
880f61be273SBen Dooks #ifdef CONFIG_MFD_SM501_GPIO
881f61be273SBen Dooks
sm501_gpio_to_dev(struct sm501_gpio * gpio)882f61be273SBen Dooks static inline struct sm501_devdata *sm501_gpio_to_dev(struct sm501_gpio *gpio)
883f61be273SBen Dooks {
884f61be273SBen Dooks return container_of(gpio, struct sm501_devdata, gpio);
885f61be273SBen Dooks }
886f61be273SBen Dooks
sm501_gpio_get(struct gpio_chip * chip,unsigned offset)887f61be273SBen Dooks static int sm501_gpio_get(struct gpio_chip *chip, unsigned offset)
888f61be273SBen Dooks
889f61be273SBen Dooks {
8903a504105SLinus Walleij struct sm501_gpio_chip *smgpio = gpiochip_get_data(chip);
891f61be273SBen Dooks unsigned long result;
892f61be273SBen Dooks
893bf5f0019SHeiko Schocher result = smc501_readl(smgpio->regbase + SM501_GPIO_DATA_LOW);
894f61be273SBen Dooks result >>= offset;
895f61be273SBen Dooks
896f61be273SBen Dooks return result & 1UL;
897f61be273SBen Dooks }
898f61be273SBen Dooks
sm501_gpio_ensure_gpio(struct sm501_gpio_chip * smchip,unsigned long bit)89998325f8fSBen Dooks static void sm501_gpio_ensure_gpio(struct sm501_gpio_chip *smchip,
90098325f8fSBen Dooks unsigned long bit)
90198325f8fSBen Dooks {
90298325f8fSBen Dooks unsigned long ctrl;
90398325f8fSBen Dooks
90498325f8fSBen Dooks /* check and modify if this pin is not set as gpio. */
90598325f8fSBen Dooks
906bf5f0019SHeiko Schocher if (smc501_readl(smchip->control) & bit) {
90798325f8fSBen Dooks dev_info(sm501_gpio_to_dev(smchip->ourgpio)->dev,
90898325f8fSBen Dooks "changing mode of gpio, bit %08lx\n", bit);
90998325f8fSBen Dooks
910bf5f0019SHeiko Schocher ctrl = smc501_readl(smchip->control);
91198325f8fSBen Dooks ctrl &= ~bit;
912bf5f0019SHeiko Schocher smc501_writel(ctrl, smchip->control);
91398325f8fSBen Dooks
91498325f8fSBen Dooks sm501_sync_regs(sm501_gpio_to_dev(smchip->ourgpio));
91598325f8fSBen Dooks }
91698325f8fSBen Dooks }
91798325f8fSBen Dooks
sm501_gpio_set(struct gpio_chip * chip,unsigned offset,int value)918f61be273SBen Dooks static void sm501_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
919f61be273SBen Dooks
920f61be273SBen Dooks {
9213a504105SLinus Walleij struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
922f61be273SBen Dooks struct sm501_gpio *smgpio = smchip->ourgpio;
923f61be273SBen Dooks unsigned long bit = 1 << offset;
924f61be273SBen Dooks void __iomem *regs = smchip->regbase;
925f61be273SBen Dooks unsigned long save;
926f61be273SBen Dooks unsigned long val;
927f61be273SBen Dooks
928f61be273SBen Dooks dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
929f61be273SBen Dooks __func__, chip, offset);
930f61be273SBen Dooks
931f61be273SBen Dooks spin_lock_irqsave(&smgpio->lock, save);
932f61be273SBen Dooks
933bf5f0019SHeiko Schocher val = smc501_readl(regs + SM501_GPIO_DATA_LOW) & ~bit;
934f61be273SBen Dooks if (value)
935f61be273SBen Dooks val |= bit;
936bf5f0019SHeiko Schocher smc501_writel(val, regs);
937f61be273SBen Dooks
938f61be273SBen Dooks sm501_sync_regs(sm501_gpio_to_dev(smgpio));
93998325f8fSBen Dooks sm501_gpio_ensure_gpio(smchip, bit);
94098325f8fSBen Dooks
941f61be273SBen Dooks spin_unlock_irqrestore(&smgpio->lock, save);
942f61be273SBen Dooks }
943f61be273SBen Dooks
sm501_gpio_input(struct gpio_chip * chip,unsigned offset)944f61be273SBen Dooks static int sm501_gpio_input(struct gpio_chip *chip, unsigned offset)
945f61be273SBen Dooks {
9463a504105SLinus Walleij struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
947f61be273SBen Dooks struct sm501_gpio *smgpio = smchip->ourgpio;
948f61be273SBen Dooks void __iomem *regs = smchip->regbase;
949f61be273SBen Dooks unsigned long bit = 1 << offset;
950f61be273SBen Dooks unsigned long save;
951f61be273SBen Dooks unsigned long ddr;
952f61be273SBen Dooks
95398325f8fSBen Dooks dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
954f61be273SBen Dooks __func__, chip, offset);
955f61be273SBen Dooks
956f61be273SBen Dooks spin_lock_irqsave(&smgpio->lock, save);
957f61be273SBen Dooks
958bf5f0019SHeiko Schocher ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
959bf5f0019SHeiko Schocher smc501_writel(ddr & ~bit, regs + SM501_GPIO_DDR_LOW);
960f61be273SBen Dooks
961f61be273SBen Dooks sm501_sync_regs(sm501_gpio_to_dev(smgpio));
96298325f8fSBen Dooks sm501_gpio_ensure_gpio(smchip, bit);
96398325f8fSBen Dooks
964f61be273SBen Dooks spin_unlock_irqrestore(&smgpio->lock, save);
965f61be273SBen Dooks
966f61be273SBen Dooks return 0;
967f61be273SBen Dooks }
968f61be273SBen Dooks
sm501_gpio_output(struct gpio_chip * chip,unsigned offset,int value)969f61be273SBen Dooks static int sm501_gpio_output(struct gpio_chip *chip,
970f61be273SBen Dooks unsigned offset, int value)
971f61be273SBen Dooks {
9723a504105SLinus Walleij struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
973f61be273SBen Dooks struct sm501_gpio *smgpio = smchip->ourgpio;
974f61be273SBen Dooks unsigned long bit = 1 << offset;
975f61be273SBen Dooks void __iomem *regs = smchip->regbase;
976f61be273SBen Dooks unsigned long save;
977f61be273SBen Dooks unsigned long val;
978f61be273SBen Dooks unsigned long ddr;
979f61be273SBen Dooks
980f61be273SBen Dooks dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d,%d)\n",
981f61be273SBen Dooks __func__, chip, offset, value);
982f61be273SBen Dooks
983f61be273SBen Dooks spin_lock_irqsave(&smgpio->lock, save);
984f61be273SBen Dooks
985bf5f0019SHeiko Schocher val = smc501_readl(regs + SM501_GPIO_DATA_LOW);
986f61be273SBen Dooks if (value)
987f61be273SBen Dooks val |= bit;
988f61be273SBen Dooks else
989f61be273SBen Dooks val &= ~bit;
990bf5f0019SHeiko Schocher smc501_writel(val, regs);
991f61be273SBen Dooks
992bf5f0019SHeiko Schocher ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
993bf5f0019SHeiko Schocher smc501_writel(ddr | bit, regs + SM501_GPIO_DDR_LOW);
994f61be273SBen Dooks
995f61be273SBen Dooks sm501_sync_regs(sm501_gpio_to_dev(smgpio));
996bf5f0019SHeiko Schocher smc501_writel(val, regs + SM501_GPIO_DATA_LOW);
997f61be273SBen Dooks
998f61be273SBen Dooks sm501_sync_regs(sm501_gpio_to_dev(smgpio));
999f61be273SBen Dooks spin_unlock_irqrestore(&smgpio->lock, save);
1000f61be273SBen Dooks
1001f61be273SBen Dooks return 0;
1002f61be273SBen Dooks }
1003f61be273SBen Dooks
10047e94e515SJulia Lawall static const struct gpio_chip gpio_chip_template = {
1005f61be273SBen Dooks .ngpio = 32,
1006f61be273SBen Dooks .direction_input = sm501_gpio_input,
1007f61be273SBen Dooks .direction_output = sm501_gpio_output,
1008f61be273SBen Dooks .set = sm501_gpio_set,
1009f61be273SBen Dooks .get = sm501_gpio_get,
1010f61be273SBen Dooks };
1011f61be273SBen Dooks
sm501_gpio_register_chip(struct sm501_devdata * sm,struct sm501_gpio * gpio,struct sm501_gpio_chip * chip)1012f791be49SBill Pemberton static int sm501_gpio_register_chip(struct sm501_devdata *sm,
1013f61be273SBen Dooks struct sm501_gpio *gpio,
1014f61be273SBen Dooks struct sm501_gpio_chip *chip)
1015f61be273SBen Dooks {
1016f61be273SBen Dooks struct sm501_platdata *pdata = sm->platdata;
1017f61be273SBen Dooks struct gpio_chip *gchip = &chip->gpio;
101860e540d6SArnaud Patard int base = pdata->gpio_base;
1019f61be273SBen Dooks
102028130beaSBen Dooks chip->gpio = gpio_chip_template;
1021f61be273SBen Dooks
1022f61be273SBen Dooks if (chip == &gpio->high) {
102360e540d6SArnaud Patard if (base > 0)
1024f61be273SBen Dooks base += 32;
1025f61be273SBen Dooks chip->regbase = gpio->regs + SM501_GPIO_DATA_HIGH;
102698325f8fSBen Dooks chip->control = sm->regs + SM501_GPIO63_32_CONTROL;
1027f61be273SBen Dooks gchip->label = "SM501-HIGH";
1028f61be273SBen Dooks } else {
1029f61be273SBen Dooks chip->regbase = gpio->regs + SM501_GPIO_DATA_LOW;
103098325f8fSBen Dooks chip->control = sm->regs + SM501_GPIO31_0_CONTROL;
1031f61be273SBen Dooks gchip->label = "SM501-LOW";
1032f61be273SBen Dooks }
1033f61be273SBen Dooks
1034f61be273SBen Dooks gchip->base = base;
1035f61be273SBen Dooks chip->ourgpio = gpio;
1036f61be273SBen Dooks
10373a504105SLinus Walleij return gpiochip_add_data(gchip, chip);
1038f61be273SBen Dooks }
1039f61be273SBen Dooks
sm501_register_gpio(struct sm501_devdata * sm)1040f791be49SBill Pemberton static int sm501_register_gpio(struct sm501_devdata *sm)
1041f61be273SBen Dooks {
1042f61be273SBen Dooks struct sm501_gpio *gpio = &sm->gpio;
1043f61be273SBen Dooks resource_size_t iobase = sm->io_res->start + SM501_GPIO;
1044f61be273SBen Dooks int ret;
1045f61be273SBen Dooks
1046f61be273SBen Dooks dev_dbg(sm->dev, "registering gpio block %08llx\n",
1047f61be273SBen Dooks (unsigned long long)iobase);
1048f61be273SBen Dooks
1049f61be273SBen Dooks spin_lock_init(&gpio->lock);
1050f61be273SBen Dooks
1051f61be273SBen Dooks gpio->regs_res = request_mem_region(iobase, 0x20, "sm501-gpio");
10524202151fSMarkus Elfring if (!gpio->regs_res) {
1053f61be273SBen Dooks dev_err(sm->dev, "gpio: failed to request region\n");
1054f61be273SBen Dooks return -ENXIO;
1055f61be273SBen Dooks }
1056f61be273SBen Dooks
1057f61be273SBen Dooks gpio->regs = ioremap(iobase, 0x20);
10584202151fSMarkus Elfring if (!gpio->regs) {
1059f61be273SBen Dooks dev_err(sm->dev, "gpio: failed to remap registers\n");
1060f61be273SBen Dooks ret = -ENXIO;
106128130beaSBen Dooks goto err_claimed;
1062f61be273SBen Dooks }
1063f61be273SBen Dooks
1064f61be273SBen Dooks /* Register both our chips. */
1065f61be273SBen Dooks
1066f61be273SBen Dooks ret = sm501_gpio_register_chip(sm, gpio, &gpio->low);
1067f61be273SBen Dooks if (ret) {
1068f61be273SBen Dooks dev_err(sm->dev, "failed to add low chip\n");
1069f61be273SBen Dooks goto err_mapped;
1070f61be273SBen Dooks }
1071f61be273SBen Dooks
1072f61be273SBen Dooks ret = sm501_gpio_register_chip(sm, gpio, &gpio->high);
1073f61be273SBen Dooks if (ret) {
1074f61be273SBen Dooks dev_err(sm->dev, "failed to add high chip\n");
1075f61be273SBen Dooks goto err_low_chip;
1076f61be273SBen Dooks }
1077f61be273SBen Dooks
1078f61be273SBen Dooks gpio->registered = 1;
1079f61be273SBen Dooks
1080f61be273SBen Dooks return 0;
1081f61be273SBen Dooks
1082f61be273SBen Dooks err_low_chip:
108388d5e520Sabdoulaye berthe gpiochip_remove(&gpio->low.gpio);
1084f61be273SBen Dooks
1085f61be273SBen Dooks err_mapped:
108628130beaSBen Dooks iounmap(gpio->regs);
108728130beaSBen Dooks
108828130beaSBen Dooks err_claimed:
1089b88aa850SChuhong Yuan release_mem_region(iobase, 0x20);
1090f61be273SBen Dooks
1091f61be273SBen Dooks return ret;
1092f61be273SBen Dooks }
1093f61be273SBen Dooks
sm501_gpio_remove(struct sm501_devdata * sm)1094f61be273SBen Dooks static void sm501_gpio_remove(struct sm501_devdata *sm)
1095f61be273SBen Dooks {
109628130beaSBen Dooks struct sm501_gpio *gpio = &sm->gpio;
1097b88aa850SChuhong Yuan resource_size_t iobase = sm->io_res->start + SM501_GPIO;
1098f61be273SBen Dooks
1099f2999209SBen Dooks if (!sm->gpio.registered)
1100f2999209SBen Dooks return;
1101f2999209SBen Dooks
110288d5e520Sabdoulaye berthe gpiochip_remove(&gpio->low.gpio);
110388d5e520Sabdoulaye berthe gpiochip_remove(&gpio->high.gpio);
110428130beaSBen Dooks
110528130beaSBen Dooks iounmap(gpio->regs);
1106b88aa850SChuhong Yuan release_mem_region(iobase, 0x20);
1107f61be273SBen Dooks }
1108f61be273SBen Dooks
sm501_gpio_isregistered(struct sm501_devdata * sm)1109f2999209SBen Dooks static inline int sm501_gpio_isregistered(struct sm501_devdata *sm)
1110f2999209SBen Dooks {
1111f2999209SBen Dooks return sm->gpio.registered;
1112f2999209SBen Dooks }
1113f61be273SBen Dooks #else
sm501_register_gpio(struct sm501_devdata * sm)111428130beaSBen Dooks static inline int sm501_register_gpio(struct sm501_devdata *sm)
1115f61be273SBen Dooks {
1116f61be273SBen Dooks return 0;
1117f61be273SBen Dooks }
1118f61be273SBen Dooks
sm501_gpio_remove(struct sm501_devdata * sm)111928130beaSBen Dooks static inline void sm501_gpio_remove(struct sm501_devdata *sm)
1120f61be273SBen Dooks {
1121f61be273SBen Dooks }
112242cd2366SBen Dooks
sm501_gpio_isregistered(struct sm501_devdata * sm)1123f2999209SBen Dooks static inline int sm501_gpio_isregistered(struct sm501_devdata *sm)
1124f2999209SBen Dooks {
1125f2999209SBen Dooks return 0;
1126f2999209SBen Dooks }
1127f61be273SBen Dooks #endif
1128f61be273SBen Dooks
sm501_register_gpio_i2c_instance(struct sm501_devdata * sm,struct sm501_platdata_gpio_i2c * iic)112942cd2366SBen Dooks static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm,
113042cd2366SBen Dooks struct sm501_platdata_gpio_i2c *iic)
113142cd2366SBen Dooks {
113242cd2366SBen Dooks struct i2c_gpio_platform_data *icd;
113342cd2366SBen Dooks struct platform_device *pdev;
1134b2e63555SLinus Walleij struct gpiod_lookup_table *lookup;
113542cd2366SBen Dooks
113642cd2366SBen Dooks pdev = sm501_create_subdev(sm, "i2c-gpio", 0,
113742cd2366SBen Dooks sizeof(struct i2c_gpio_platform_data));
113842cd2366SBen Dooks if (!pdev)
113942cd2366SBen Dooks return -ENOMEM;
114042cd2366SBen Dooks
1141b2e63555SLinus Walleij /* Create a gpiod lookup using gpiochip-local offsets */
11429f208ecaSGustavo A. R. Silva lookup = devm_kzalloc(&pdev->dev, struct_size(lookup, table, 3),
1143b2e63555SLinus Walleij GFP_KERNEL);
1144ae7b8edaSGustavo A. R. Silva if (!lookup)
1145ae7b8edaSGustavo A. R. Silva return -ENOMEM;
1146ae7b8edaSGustavo A. R. Silva
1147b2e63555SLinus Walleij lookup->dev_id = "i2c-gpio";
11487b67b836SGeert Uytterhoeven lookup->table[0] = (struct gpiod_lookup)
11497b67b836SGeert Uytterhoeven GPIO_LOOKUP_IDX(iic->pin_sda < 32 ? "SM501-LOW" : "SM501-HIGH",
11507b67b836SGeert Uytterhoeven iic->pin_sda % 32, NULL, 0,
11517b67b836SGeert Uytterhoeven GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN);
11527b67b836SGeert Uytterhoeven lookup->table[1] = (struct gpiod_lookup)
11537b67b836SGeert Uytterhoeven GPIO_LOOKUP_IDX(iic->pin_scl < 32 ? "SM501-LOW" : "SM501-HIGH",
11547b67b836SGeert Uytterhoeven iic->pin_scl % 32, NULL, 1,
11557b67b836SGeert Uytterhoeven GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN);
1156b2e63555SLinus Walleij gpiod_add_lookup_table(lookup);
1157b2e63555SLinus Walleij
1158334a41ceSJingoo Han icd = dev_get_platdata(&pdev->dev);
115942cd2366SBen Dooks icd->timeout = iic->timeout;
116042cd2366SBen Dooks icd->udelay = iic->udelay;
116142cd2366SBen Dooks
116242cd2366SBen Dooks /* note, we can't use either of the pin numbers, as the i2c-gpio
116342cd2366SBen Dooks * driver uses the platform.id field to generate the bus number
116442cd2366SBen Dooks * to register with the i2c core; The i2c core doesn't have enough
116542cd2366SBen Dooks * entries to deal with anything we currently use.
116642cd2366SBen Dooks */
116742cd2366SBen Dooks
116842cd2366SBen Dooks pdev->id = iic->bus_num;
116942cd2366SBen Dooks
1170b2e63555SLinus Walleij dev_info(sm->dev, "registering i2c-%d: sda=%d, scl=%d\n",
117142cd2366SBen Dooks iic->bus_num,
1172b2e63555SLinus Walleij iic->pin_sda, iic->pin_scl);
117342cd2366SBen Dooks
117442cd2366SBen Dooks return sm501_register_device(sm, pdev);
117542cd2366SBen Dooks }
117642cd2366SBen Dooks
sm501_register_gpio_i2c(struct sm501_devdata * sm,struct sm501_platdata * pdata)117742cd2366SBen Dooks static int sm501_register_gpio_i2c(struct sm501_devdata *sm,
117842cd2366SBen Dooks struct sm501_platdata *pdata)
117942cd2366SBen Dooks {
118042cd2366SBen Dooks struct sm501_platdata_gpio_i2c *iic = pdata->gpio_i2c;
118142cd2366SBen Dooks int index;
118242cd2366SBen Dooks int ret;
118342cd2366SBen Dooks
118442cd2366SBen Dooks for (index = 0; index < pdata->gpio_i2c_nr; index++, iic++) {
118542cd2366SBen Dooks ret = sm501_register_gpio_i2c_instance(sm, iic);
118642cd2366SBen Dooks if (ret < 0)
118742cd2366SBen Dooks return ret;
118842cd2366SBen Dooks }
118942cd2366SBen Dooks
119042cd2366SBen Dooks return 0;
119142cd2366SBen Dooks }
119242cd2366SBen Dooks
1193a2cb660bSZhen Lei /* dbg_regs_show
1194b6d6454fSBen Dooks *
1195b6d6454fSBen Dooks * Debug attribute to attach to parent device to show core registers
1196b6d6454fSBen Dooks */
1197b6d6454fSBen Dooks
dbg_regs_show(struct device * dev,struct device_attribute * attr,char * buff)1198a2cb660bSZhen Lei static ssize_t dbg_regs_show(struct device *dev,
1199b6d6454fSBen Dooks struct device_attribute *attr, char *buff)
1200b6d6454fSBen Dooks {
1201b6d6454fSBen Dooks struct sm501_devdata *sm = dev_get_drvdata(dev) ;
1202b6d6454fSBen Dooks unsigned int reg;
1203b6d6454fSBen Dooks char *ptr = buff;
1204b6d6454fSBen Dooks int ret;
1205b6d6454fSBen Dooks
1206b6d6454fSBen Dooks for (reg = 0x00; reg < 0x70; reg += 4) {
1207b6d6454fSBen Dooks ret = sprintf(ptr, "%08x = %08x\n",
1208bf5f0019SHeiko Schocher reg, smc501_readl(sm->regs + reg));
1209b6d6454fSBen Dooks ptr += ret;
1210b6d6454fSBen Dooks }
1211b6d6454fSBen Dooks
1212b6d6454fSBen Dooks return ptr - buff;
1213b6d6454fSBen Dooks }
1214b6d6454fSBen Dooks
1215b6d6454fSBen Dooks
1216a2cb660bSZhen Lei static DEVICE_ATTR_RO(dbg_regs);
1217b6d6454fSBen Dooks
1218b6d6454fSBen Dooks /* sm501_init_reg
1219b6d6454fSBen Dooks *
1220b6d6454fSBen Dooks * Helper function for the init code to setup a register
12215136237bSBen Dooks *
12225136237bSBen Dooks * clear the bits which are set in r->mask, and then set
12235136237bSBen Dooks * the bits set in r->set.
1224b6d6454fSBen Dooks */
1225b6d6454fSBen Dooks
sm501_init_reg(struct sm501_devdata * sm,unsigned long reg,struct sm501_reg_init * r)1226b6d6454fSBen Dooks static inline void sm501_init_reg(struct sm501_devdata *sm,
1227b6d6454fSBen Dooks unsigned long reg,
1228b6d6454fSBen Dooks struct sm501_reg_init *r)
1229b6d6454fSBen Dooks {
1230b6d6454fSBen Dooks unsigned long tmp;
1231b6d6454fSBen Dooks
1232bf5f0019SHeiko Schocher tmp = smc501_readl(sm->regs + reg);
1233b6d6454fSBen Dooks tmp &= ~r->mask;
12345136237bSBen Dooks tmp |= r->set;
1235bf5f0019SHeiko Schocher smc501_writel(tmp, sm->regs + reg);
1236b6d6454fSBen Dooks }
1237b6d6454fSBen Dooks
1238b6d6454fSBen Dooks /* sm501_init_regs
1239b6d6454fSBen Dooks *
1240b6d6454fSBen Dooks * Setup core register values
1241b6d6454fSBen Dooks */
1242b6d6454fSBen Dooks
sm501_init_regs(struct sm501_devdata * sm,struct sm501_initdata * init)1243b6d6454fSBen Dooks static void sm501_init_regs(struct sm501_devdata *sm,
1244b6d6454fSBen Dooks struct sm501_initdata *init)
1245b6d6454fSBen Dooks {
1246b6d6454fSBen Dooks sm501_misc_control(sm->dev,
1247b6d6454fSBen Dooks init->misc_control.set,
1248b6d6454fSBen Dooks init->misc_control.mask);
1249b6d6454fSBen Dooks
1250b6d6454fSBen Dooks sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
1251b6d6454fSBen Dooks sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
1252b6d6454fSBen Dooks sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
1253b6d6454fSBen Dooks
1254b6d6454fSBen Dooks if (init->m1xclk) {
1255b6d6454fSBen Dooks dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
1256b6d6454fSBen Dooks sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
1257b6d6454fSBen Dooks }
1258b5913bbdSBen Dooks
1259b5913bbdSBen Dooks if (init->mclk) {
1260b5913bbdSBen Dooks dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
1261b5913bbdSBen Dooks sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
1262b5913bbdSBen Dooks }
126381906221SBen Dooks
126481906221SBen Dooks }
126581906221SBen Dooks
126681906221SBen Dooks /* Check the PLL sources for the M1CLK and M1XCLK
126781906221SBen Dooks *
126881906221SBen Dooks * If the M1CLK and M1XCLKs are not sourced from the same PLL, then
126981906221SBen Dooks * there is a risk (see errata AB-5) that the SM501 will cease proper
127081906221SBen Dooks * function. If this happens, then it is likely the SM501 will
127181906221SBen Dooks * hang the system.
127281906221SBen Dooks */
127381906221SBen Dooks
sm501_check_clocks(struct sm501_devdata * sm)127481906221SBen Dooks static int sm501_check_clocks(struct sm501_devdata *sm)
127581906221SBen Dooks {
1276bf5f0019SHeiko Schocher unsigned long pwrmode = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
127781906221SBen Dooks unsigned long msrc = (pwrmode & SM501_POWERMODE_M_SRC);
127881906221SBen Dooks unsigned long m1src = (pwrmode & SM501_POWERMODE_M1_SRC);
127981906221SBen Dooks
128081906221SBen Dooks return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
1281b6d6454fSBen Dooks }
1282b6d6454fSBen Dooks
1283b6d6454fSBen Dooks static unsigned int sm501_mem_local[] = {
1284b6d6454fSBen Dooks [0] = 4*1024*1024,
1285b6d6454fSBen Dooks [1] = 8*1024*1024,
1286b6d6454fSBen Dooks [2] = 16*1024*1024,
1287b6d6454fSBen Dooks [3] = 32*1024*1024,
1288b6d6454fSBen Dooks [4] = 64*1024*1024,
1289b6d6454fSBen Dooks [5] = 2*1024*1024,
1290b6d6454fSBen Dooks };
1291b6d6454fSBen Dooks
1292b6d6454fSBen Dooks /* sm501_init_dev
1293b6d6454fSBen Dooks *
1294b6d6454fSBen Dooks * Common init code for an SM501
1295b6d6454fSBen Dooks */
1296b6d6454fSBen Dooks
sm501_init_dev(struct sm501_devdata * sm)1297f791be49SBill Pemberton static int sm501_init_dev(struct sm501_devdata *sm)
1298b6d6454fSBen Dooks {
129961711f8fSMagnus Damm struct sm501_initdata *idata;
130042cd2366SBen Dooks struct sm501_platdata *pdata;
1301b6d6454fSBen Dooks resource_size_t mem_avail;
1302b6d6454fSBen Dooks unsigned long dramctrl;
13031e27dbe7SBen Dooks unsigned long devid;
1304b6d6454fSBen Dooks int ret;
1305b6d6454fSBen Dooks
1306b6d6454fSBen Dooks mutex_init(&sm->clock_lock);
1307b6d6454fSBen Dooks spin_lock_init(&sm->reg_lock);
1308b6d6454fSBen Dooks
1309b6d6454fSBen Dooks INIT_LIST_HEAD(&sm->devices);
1310b6d6454fSBen Dooks
1311bf5f0019SHeiko Schocher devid = smc501_readl(sm->regs + SM501_DEVICEID);
1312b6d6454fSBen Dooks
13131e27dbe7SBen Dooks if ((devid & SM501_DEVICEID_IDMASK) != SM501_DEVICEID_SM501) {
13141e27dbe7SBen Dooks dev_err(sm->dev, "incorrect device id %08lx\n", devid);
13151e27dbe7SBen Dooks return -EINVAL;
13161e27dbe7SBen Dooks }
13171e27dbe7SBen Dooks
131861711f8fSMagnus Damm /* disable irqs */
1319bf5f0019SHeiko Schocher smc501_writel(0, sm->regs + SM501_IRQ_MASK);
132061711f8fSMagnus Damm
1321bf5f0019SHeiko Schocher dramctrl = smc501_readl(sm->regs + SM501_DRAM_CONTROL);
1322b6d6454fSBen Dooks mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
1323b6d6454fSBen Dooks
13241e27dbe7SBen Dooks dev_info(sm->dev, "SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
13251e27dbe7SBen Dooks sm->regs, devid, (unsigned long)mem_avail >> 20, sm->irq);
1326b6d6454fSBen Dooks
13273149be50SVille Syrjala sm->rev = devid & SM501_DEVICEID_REVMASK;
13283149be50SVille Syrjala
1329331d7475SBen Dooks sm501_dump_gate(sm);
1330b6d6454fSBen Dooks
1331b6d6454fSBen Dooks ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
1332b6d6454fSBen Dooks if (ret)
1333b6d6454fSBen Dooks dev_err(sm->dev, "failed to create debug regs file\n");
1334b6d6454fSBen Dooks
1335b6d6454fSBen Dooks sm501_dump_clk(sm);
1336b6d6454fSBen Dooks
1337b6d6454fSBen Dooks /* check to see if we have some device initialisation */
1338b6d6454fSBen Dooks
133942cd2366SBen Dooks pdata = sm->platdata;
134042cd2366SBen Dooks idata = pdata ? pdata->init : NULL;
134142cd2366SBen Dooks
134261711f8fSMagnus Damm if (idata) {
134361711f8fSMagnus Damm sm501_init_regs(sm, idata);
1344b6d6454fSBen Dooks
134561711f8fSMagnus Damm if (idata->devices & SM501_USE_USB_HOST)
1346b6d6454fSBen Dooks sm501_register_usbhost(sm, &mem_avail);
134761711f8fSMagnus Damm if (idata->devices & (SM501_USE_UART0 | SM501_USE_UART1))
134861711f8fSMagnus Damm sm501_register_uart(sm, idata->devices);
1349f61be273SBen Dooks if (idata->devices & SM501_USE_GPIO)
1350f61be273SBen Dooks sm501_register_gpio(sm);
1351b6d6454fSBen Dooks }
1352b6d6454fSBen Dooks
13534202151fSMarkus Elfring if (pdata && pdata->gpio_i2c && pdata->gpio_i2c_nr > 0) {
1354f2999209SBen Dooks if (!sm501_gpio_isregistered(sm))
1355f2999209SBen Dooks dev_err(sm->dev, "no gpio available for i2c gpio.\n");
135642cd2366SBen Dooks else
135742cd2366SBen Dooks sm501_register_gpio_i2c(sm, pdata);
135842cd2366SBen Dooks }
135942cd2366SBen Dooks
136081906221SBen Dooks ret = sm501_check_clocks(sm);
136181906221SBen Dooks if (ret) {
136281906221SBen Dooks dev_err(sm->dev, "M1X and M clocks sourced from different "
136381906221SBen Dooks "PLLs\n");
136481906221SBen Dooks return -EINVAL;
136581906221SBen Dooks }
136681906221SBen Dooks
1367b6d6454fSBen Dooks /* always create a framebuffer */
1368b6d6454fSBen Dooks sm501_register_display(sm, &mem_avail);
1369b6d6454fSBen Dooks
1370b6d6454fSBen Dooks return 0;
1371b6d6454fSBen Dooks }
1372b6d6454fSBen Dooks
sm501_plat_probe(struct platform_device * dev)1373f791be49SBill Pemberton static int sm501_plat_probe(struct platform_device *dev)
1374b6d6454fSBen Dooks {
1375b6d6454fSBen Dooks struct sm501_devdata *sm;
13767cf5244cSRoel Kluin int ret;
1377b6d6454fSBen Dooks
13783eec4fadSMarkus Elfring sm = kzalloc(sizeof(*sm), GFP_KERNEL);
13794202151fSMarkus Elfring if (!sm) {
13807cf5244cSRoel Kluin ret = -ENOMEM;
1381b6d6454fSBen Dooks goto err1;
1382b6d6454fSBen Dooks }
1383b6d6454fSBen Dooks
1384b6d6454fSBen Dooks sm->dev = &dev->dev;
1385b6d6454fSBen Dooks sm->pdev_id = dev->id;
1386334a41ceSJingoo Han sm->platdata = dev_get_platdata(&dev->dev);
1387b6d6454fSBen Dooks
13887cf5244cSRoel Kluin ret = platform_get_irq(dev, 0);
1389802d9bd4SStephen Boyd if (ret < 0)
1390b6d6454fSBen Dooks goto err_res;
13917cf5244cSRoel Kluin sm->irq = ret;
1392b6d6454fSBen Dooks
13937cf5244cSRoel Kluin sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
13947cf5244cSRoel Kluin sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
13954202151fSMarkus Elfring if (!sm->io_res || !sm->mem_res) {
1396b6d6454fSBen Dooks dev_err(&dev->dev, "failed to get IO resource\n");
13977cf5244cSRoel Kluin ret = -ENOENT;
1398b6d6454fSBen Dooks goto err_res;
1399b6d6454fSBen Dooks }
1400b6d6454fSBen Dooks
1401b6d6454fSBen Dooks sm->regs_claim = request_mem_region(sm->io_res->start,
1402a5300dcbSSamuel Ortiz 0x100, "sm501");
14034202151fSMarkus Elfring if (!sm->regs_claim) {
1404b6d6454fSBen Dooks dev_err(&dev->dev, "cannot claim registers\n");
14057cf5244cSRoel Kluin ret = -EBUSY;
1406b6d6454fSBen Dooks goto err_res;
1407b6d6454fSBen Dooks }
1408b6d6454fSBen Dooks
1409b6d6454fSBen Dooks platform_set_drvdata(dev, sm);
1410b6d6454fSBen Dooks
1411311e54c0SH Hartley Sweeten sm->regs = ioremap(sm->io_res->start, resource_size(sm->io_res));
14124202151fSMarkus Elfring if (!sm->regs) {
1413b6d6454fSBen Dooks dev_err(&dev->dev, "cannot remap registers\n");
14147cf5244cSRoel Kluin ret = -EIO;
1415b6d6454fSBen Dooks goto err_claim;
1416b6d6454fSBen Dooks }
1417b6d6454fSBen Dooks
14188ce24f89SDan Carpenter ret = sm501_init_dev(sm);
14198ce24f89SDan Carpenter if (ret)
14208ce24f89SDan Carpenter goto err_unmap;
1421b6d6454fSBen Dooks
14228ce24f89SDan Carpenter return 0;
14238ce24f89SDan Carpenter
14248ce24f89SDan Carpenter err_unmap:
14258ce24f89SDan Carpenter iounmap(sm->regs);
1426b6d6454fSBen Dooks err_claim:
1427b88aa850SChuhong Yuan release_mem_region(sm->io_res->start, 0x100);
1428b6d6454fSBen Dooks err_res:
1429b6d6454fSBen Dooks kfree(sm);
1430b6d6454fSBen Dooks err1:
14317cf5244cSRoel Kluin return ret;
1432b6d6454fSBen Dooks
1433b6d6454fSBen Dooks }
1434b6d6454fSBen Dooks
1435331d7475SBen Dooks /* power management support */
1436331d7475SBen Dooks
sm501_set_power(struct sm501_devdata * sm,int on)1437472dba7dSBen Dooks static void sm501_set_power(struct sm501_devdata *sm, int on)
1438472dba7dSBen Dooks {
1439472dba7dSBen Dooks struct sm501_platdata *pd = sm->platdata;
1440472dba7dSBen Dooks
14414202151fSMarkus Elfring if (!pd)
1442472dba7dSBen Dooks return;
1443472dba7dSBen Dooks
1444472dba7dSBen Dooks if (pd->get_power) {
1445472dba7dSBen Dooks if (pd->get_power(sm->dev) == on) {
1446472dba7dSBen Dooks dev_dbg(sm->dev, "is already %d\n", on);
1447472dba7dSBen Dooks return;
1448472dba7dSBen Dooks }
1449472dba7dSBen Dooks }
1450472dba7dSBen Dooks
1451472dba7dSBen Dooks if (pd->set_power) {
1452472dba7dSBen Dooks dev_dbg(sm->dev, "setting power to %d\n", on);
1453472dba7dSBen Dooks
1454472dba7dSBen Dooks pd->set_power(sm->dev, on);
1455472dba7dSBen Dooks sm501_mdelay(sm, 10);
1456472dba7dSBen Dooks }
1457472dba7dSBen Dooks }
1458472dba7dSBen Dooks
sm501_plat_suspend(struct platform_device * pdev,pm_message_t state)1459331d7475SBen Dooks static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
1460331d7475SBen Dooks {
1461331d7475SBen Dooks struct sm501_devdata *sm = platform_get_drvdata(pdev);
1462331d7475SBen Dooks
1463331d7475SBen Dooks sm->in_suspend = 1;
1464bf5f0019SHeiko Schocher sm->pm_misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
1465331d7475SBen Dooks
1466331d7475SBen Dooks sm501_dump_regs(sm);
1467472dba7dSBen Dooks
1468472dba7dSBen Dooks if (sm->platdata) {
1469472dba7dSBen Dooks if (sm->platdata->flags & SM501_FLAG_SUSPEND_OFF)
1470472dba7dSBen Dooks sm501_set_power(sm, 0);
1471472dba7dSBen Dooks }
1472472dba7dSBen Dooks
1473331d7475SBen Dooks return 0;
1474331d7475SBen Dooks }
1475331d7475SBen Dooks
sm501_plat_resume(struct platform_device * pdev)1476331d7475SBen Dooks static int sm501_plat_resume(struct platform_device *pdev)
1477331d7475SBen Dooks {
1478331d7475SBen Dooks struct sm501_devdata *sm = platform_get_drvdata(pdev);
1479331d7475SBen Dooks
1480472dba7dSBen Dooks sm501_set_power(sm, 1);
1481472dba7dSBen Dooks
1482331d7475SBen Dooks sm501_dump_regs(sm);
1483331d7475SBen Dooks sm501_dump_gate(sm);
1484331d7475SBen Dooks sm501_dump_clk(sm);
1485331d7475SBen Dooks
1486331d7475SBen Dooks /* check to see if we are in the same state as when suspended */
1487331d7475SBen Dooks
1488bf5f0019SHeiko Schocher if (smc501_readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
1489331d7475SBen Dooks dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
1490bf5f0019SHeiko Schocher smc501_writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
1491331d7475SBen Dooks
1492331d7475SBen Dooks /* our suspend causes the controller state to change,
1493331d7475SBen Dooks * either by something attempting setup, power loss,
1494331d7475SBen Dooks * or an external reset event on power change */
1495331d7475SBen Dooks
1496331d7475SBen Dooks if (sm->platdata && sm->platdata->init) {
1497331d7475SBen Dooks sm501_init_regs(sm, sm->platdata->init);
1498331d7475SBen Dooks }
1499331d7475SBen Dooks }
1500331d7475SBen Dooks
1501331d7475SBen Dooks /* dump our state from resume */
1502331d7475SBen Dooks
1503331d7475SBen Dooks sm501_dump_regs(sm);
1504331d7475SBen Dooks sm501_dump_clk(sm);
1505331d7475SBen Dooks
1506331d7475SBen Dooks sm->in_suspend = 0;
1507331d7475SBen Dooks
1508331d7475SBen Dooks return 0;
1509331d7475SBen Dooks }
1510331d7475SBen Dooks
1511b6d6454fSBen Dooks /* Initialisation data for PCI devices */
1512b6d6454fSBen Dooks
1513b6d6454fSBen Dooks static struct sm501_initdata sm501_pci_initdata = {
1514b6d6454fSBen Dooks .gpio_high = {
1515b6d6454fSBen Dooks .set = 0x3F000000, /* 24bit panel */
1516b6d6454fSBen Dooks .mask = 0x0,
1517b6d6454fSBen Dooks },
1518b6d6454fSBen Dooks .misc_timing = {
1519b6d6454fSBen Dooks .set = 0x010100, /* SDRAM timing */
1520b6d6454fSBen Dooks .mask = 0x1F1F00,
1521b6d6454fSBen Dooks },
1522b6d6454fSBen Dooks .misc_control = {
1523b6d6454fSBen Dooks .set = SM501_MISC_PNL_24BIT,
1524b6d6454fSBen Dooks .mask = 0,
1525b6d6454fSBen Dooks },
1526b6d6454fSBen Dooks
1527b6d6454fSBen Dooks .devices = SM501_USE_ALL,
152881906221SBen Dooks
152981906221SBen Dooks /* Errata AB-3 says that 72MHz is the fastest available
153081906221SBen Dooks * for 33MHZ PCI with proper bus-mastering operation */
153181906221SBen Dooks
153281906221SBen Dooks .mclk = 72 * MHZ,
153381906221SBen Dooks .m1xclk = 144 * MHZ,
1534b6d6454fSBen Dooks };
1535b6d6454fSBen Dooks
1536b6d6454fSBen Dooks static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
1537b6d6454fSBen Dooks .flags = (SM501FB_FLAG_USE_INIT_MODE |
1538b6d6454fSBen Dooks SM501FB_FLAG_USE_HWCURSOR |
1539b6d6454fSBen Dooks SM501FB_FLAG_USE_HWACCEL |
1540b6d6454fSBen Dooks SM501FB_FLAG_DISABLE_AT_EXIT),
1541b6d6454fSBen Dooks };
1542b6d6454fSBen Dooks
1543b6d6454fSBen Dooks static struct sm501_platdata_fb sm501_fb_pdata = {
1544b6d6454fSBen Dooks .fb_route = SM501_FB_OWN,
1545b6d6454fSBen Dooks .fb_crt = &sm501_pdata_fbsub,
1546b6d6454fSBen Dooks .fb_pnl = &sm501_pdata_fbsub,
1547b6d6454fSBen Dooks };
1548b6d6454fSBen Dooks
1549b6d6454fSBen Dooks static struct sm501_platdata sm501_pci_platdata = {
1550b6d6454fSBen Dooks .init = &sm501_pci_initdata,
1551b6d6454fSBen Dooks .fb = &sm501_fb_pdata,
155260e540d6SArnaud Patard .gpio_base = -1,
1553b6d6454fSBen Dooks };
1554b6d6454fSBen Dooks
sm501_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)1555f791be49SBill Pemberton static int sm501_pci_probe(struct pci_dev *dev,
1556b6d6454fSBen Dooks const struct pci_device_id *id)
1557b6d6454fSBen Dooks {
1558b6d6454fSBen Dooks struct sm501_devdata *sm;
1559b6d6454fSBen Dooks int err;
1560b6d6454fSBen Dooks
15613eec4fadSMarkus Elfring sm = kzalloc(sizeof(*sm), GFP_KERNEL);
15624202151fSMarkus Elfring if (!sm) {
1563b6d6454fSBen Dooks err = -ENOMEM;
1564b6d6454fSBen Dooks goto err1;
1565b6d6454fSBen Dooks }
1566b6d6454fSBen Dooks
1567b6d6454fSBen Dooks /* set a default set of platform data */
1568b6d6454fSBen Dooks dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
1569b6d6454fSBen Dooks
1570b6d6454fSBen Dooks /* set a hopefully unique id for our child platform devices */
1571b6d6454fSBen Dooks sm->pdev_id = 32 + dev->devfn;
1572b6d6454fSBen Dooks
1573b6d6454fSBen Dooks pci_set_drvdata(dev, sm);
1574b6d6454fSBen Dooks
1575b6d6454fSBen Dooks err = pci_enable_device(dev);
1576b6d6454fSBen Dooks if (err) {
1577b6d6454fSBen Dooks dev_err(&dev->dev, "cannot enable device\n");
1578b6d6454fSBen Dooks goto err2;
1579b6d6454fSBen Dooks }
1580b6d6454fSBen Dooks
1581b6d6454fSBen Dooks sm->dev = &dev->dev;
1582b6d6454fSBen Dooks sm->irq = dev->irq;
1583b6d6454fSBen Dooks
1584b6d6454fSBen Dooks #ifdef __BIG_ENDIAN
1585b6d6454fSBen Dooks /* if the system is big-endian, we most probably have a
1586b6d6454fSBen Dooks * translation in the IO layer making the PCI bus little endian
1587b6d6454fSBen Dooks * so make the framebuffer swapped pixels */
1588b6d6454fSBen Dooks
1589b6d6454fSBen Dooks sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
1590b6d6454fSBen Dooks #endif
1591b6d6454fSBen Dooks
1592b6d6454fSBen Dooks /* check our resources */
1593b6d6454fSBen Dooks
1594b6d6454fSBen Dooks if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
1595b6d6454fSBen Dooks dev_err(&dev->dev, "region #0 is not memory?\n");
1596b6d6454fSBen Dooks err = -EINVAL;
1597b6d6454fSBen Dooks goto err3;
1598b6d6454fSBen Dooks }
1599b6d6454fSBen Dooks
1600b6d6454fSBen Dooks if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
1601b6d6454fSBen Dooks dev_err(&dev->dev, "region #1 is not memory?\n");
1602b6d6454fSBen Dooks err = -EINVAL;
1603b6d6454fSBen Dooks goto err3;
1604b6d6454fSBen Dooks }
1605b6d6454fSBen Dooks
1606b6d6454fSBen Dooks /* make our resources ready for sharing */
1607b6d6454fSBen Dooks
1608b6d6454fSBen Dooks sm->io_res = &dev->resource[1];
1609b6d6454fSBen Dooks sm->mem_res = &dev->resource[0];
1610b6d6454fSBen Dooks
1611b6d6454fSBen Dooks sm->regs_claim = request_mem_region(sm->io_res->start,
1612a5300dcbSSamuel Ortiz 0x100, "sm501");
16134202151fSMarkus Elfring if (!sm->regs_claim) {
1614b6d6454fSBen Dooks dev_err(&dev->dev, "cannot claim registers\n");
1615b6d6454fSBen Dooks err= -EBUSY;
1616b6d6454fSBen Dooks goto err3;
1617b6d6454fSBen Dooks }
1618b6d6454fSBen Dooks
16197ab18995SArjan van de Ven sm->regs = pci_ioremap_bar(dev, 1);
16204202151fSMarkus Elfring if (!sm->regs) {
1621b6d6454fSBen Dooks dev_err(&dev->dev, "cannot remap registers\n");
1622b6d6454fSBen Dooks err = -EIO;
1623b6d6454fSBen Dooks goto err4;
1624b6d6454fSBen Dooks }
1625b6d6454fSBen Dooks
1626b6d6454fSBen Dooks sm501_init_dev(sm);
1627b6d6454fSBen Dooks return 0;
1628b6d6454fSBen Dooks
1629b6d6454fSBen Dooks err4:
1630b88aa850SChuhong Yuan release_mem_region(sm->io_res->start, 0x100);
1631b6d6454fSBen Dooks err3:
1632b6d6454fSBen Dooks pci_disable_device(dev);
1633b6d6454fSBen Dooks err2:
1634b6d6454fSBen Dooks kfree(sm);
1635b6d6454fSBen Dooks err1:
1636b6d6454fSBen Dooks return err;
1637b6d6454fSBen Dooks }
1638b6d6454fSBen Dooks
sm501_remove_sub(struct sm501_devdata * sm,struct sm501_device * smdev)1639b6d6454fSBen Dooks static void sm501_remove_sub(struct sm501_devdata *sm,
1640b6d6454fSBen Dooks struct sm501_device *smdev)
1641b6d6454fSBen Dooks {
1642b6d6454fSBen Dooks list_del(&smdev->list);
1643b6d6454fSBen Dooks platform_device_unregister(&smdev->pdev);
1644b6d6454fSBen Dooks }
1645b6d6454fSBen Dooks
sm501_dev_remove(struct sm501_devdata * sm)1646b6d6454fSBen Dooks static void sm501_dev_remove(struct sm501_devdata *sm)
1647b6d6454fSBen Dooks {
1648b6d6454fSBen Dooks struct sm501_device *smdev, *tmp;
1649b6d6454fSBen Dooks
1650b6d6454fSBen Dooks list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
1651b6d6454fSBen Dooks sm501_remove_sub(sm, smdev);
1652b6d6454fSBen Dooks
1653b6d6454fSBen Dooks device_remove_file(sm->dev, &dev_attr_dbg_regs);
1654f61be273SBen Dooks
1655f61be273SBen Dooks sm501_gpio_remove(sm);
1656b6d6454fSBen Dooks }
1657b6d6454fSBen Dooks
sm501_pci_remove(struct pci_dev * dev)16584740f73fSBill Pemberton static void sm501_pci_remove(struct pci_dev *dev)
1659b6d6454fSBen Dooks {
1660b6d6454fSBen Dooks struct sm501_devdata *sm = pci_get_drvdata(dev);
1661b6d6454fSBen Dooks
1662b6d6454fSBen Dooks sm501_dev_remove(sm);
1663b6d6454fSBen Dooks iounmap(sm->regs);
1664b6d6454fSBen Dooks
1665b88aa850SChuhong Yuan release_mem_region(sm->io_res->start, 0x100);
1666b6d6454fSBen Dooks
1667b6d6454fSBen Dooks pci_disable_device(dev);
1668b6d6454fSBen Dooks }
1669b6d6454fSBen Dooks
sm501_plat_remove(struct platform_device * dev)1670b6d6454fSBen Dooks static int sm501_plat_remove(struct platform_device *dev)
1671b6d6454fSBen Dooks {
1672b6d6454fSBen Dooks struct sm501_devdata *sm = platform_get_drvdata(dev);
1673b6d6454fSBen Dooks
1674b6d6454fSBen Dooks sm501_dev_remove(sm);
1675b6d6454fSBen Dooks iounmap(sm->regs);
1676b6d6454fSBen Dooks
1677b88aa850SChuhong Yuan release_mem_region(sm->io_res->start, 0x100);
1678b6d6454fSBen Dooks
1679b6d6454fSBen Dooks return 0;
1680b6d6454fSBen Dooks }
1681b6d6454fSBen Dooks
168236fcd06cSJingoo Han static const struct pci_device_id sm501_pci_tbl[] = {
1683b6d6454fSBen Dooks { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1684b6d6454fSBen Dooks { 0, },
1685b6d6454fSBen Dooks };
1686b6d6454fSBen Dooks
1687b6d6454fSBen Dooks MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
1688b6d6454fSBen Dooks
1689158abca5SAlexey Dobriyan static struct pci_driver sm501_pci_driver = {
1690b6d6454fSBen Dooks .name = "sm501",
1691b6d6454fSBen Dooks .id_table = sm501_pci_tbl,
1692b6d6454fSBen Dooks .probe = sm501_pci_probe,
169384449216SBill Pemberton .remove = sm501_pci_remove,
1694b6d6454fSBen Dooks };
1695b6d6454fSBen Dooks
16964f46d6e7SKay Sievers MODULE_ALIAS("platform:sm501");
16974f46d6e7SKay Sievers
1698ae6eee3cSJingoo Han static const struct of_device_id of_sm501_match_tbl[] = {
16994295f9bfSHeiko Schocher { .compatible = "smi,sm501", },
17004295f9bfSHeiko Schocher { /* end */ }
17014295f9bfSHeiko Schocher };
1702327cc18eSLuis de Bethencourt MODULE_DEVICE_TABLE(of, of_sm501_match_tbl);
17034295f9bfSHeiko Schocher
1704158abca5SAlexey Dobriyan static struct platform_driver sm501_plat_driver = {
1705b6d6454fSBen Dooks .driver = {
1706b6d6454fSBen Dooks .name = "sm501",
17074295f9bfSHeiko Schocher .of_match_table = of_sm501_match_tbl,
1708b6d6454fSBen Dooks },
1709b6d6454fSBen Dooks .probe = sm501_plat_probe,
1710b6d6454fSBen Dooks .remove = sm501_plat_remove,
1711*3833239bSPaul Cercueil .suspend = pm_sleep_ptr(sm501_plat_suspend),
1712*3833239bSPaul Cercueil .resume = pm_sleep_ptr(sm501_plat_resume),
1713b6d6454fSBen Dooks };
1714b6d6454fSBen Dooks
sm501_base_init(void)1715b6d6454fSBen Dooks static int __init sm501_base_init(void)
1716b6d6454fSBen Dooks {
17178325a6c2SJiasheng Jiang int ret;
17188325a6c2SJiasheng Jiang
17198325a6c2SJiasheng Jiang ret = platform_driver_register(&sm501_plat_driver);
17208325a6c2SJiasheng Jiang if (ret < 0)
17218325a6c2SJiasheng Jiang return ret;
17228325a6c2SJiasheng Jiang
1723158abca5SAlexey Dobriyan return pci_register_driver(&sm501_pci_driver);
1724b6d6454fSBen Dooks }
1725b6d6454fSBen Dooks
sm501_base_exit(void)1726b6d6454fSBen Dooks static void __exit sm501_base_exit(void)
1727b6d6454fSBen Dooks {
1728158abca5SAlexey Dobriyan platform_driver_unregister(&sm501_plat_driver);
1729158abca5SAlexey Dobriyan pci_unregister_driver(&sm501_pci_driver);
1730b6d6454fSBen Dooks }
1731b6d6454fSBen Dooks
1732b6d6454fSBen Dooks module_init(sm501_base_init);
1733b6d6454fSBen Dooks module_exit(sm501_base_exit);
1734b6d6454fSBen Dooks
1735b6d6454fSBen Dooks MODULE_DESCRIPTION("SM501 Core Driver");
1736b6d6454fSBen Dooks MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
1737b6d6454fSBen Dooks MODULE_LICENSE("GPL v2");
1738