blob: e49787e6bb93e5c84660d9c4a7b3b50370aab4f9 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Ben Dooksb6d64542007-02-20 13:58:01 -08002/* linux/drivers/mfd/sm501.c
3 *
4 * Copyright (C) 2006 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
6 * Vincent Sanders <vince@simtec.co.uk>
7 *
Ben Dooksb6d64542007-02-20 13:58:01 -08008 * SM501 MFD driver
9*/
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/init.h>
15#include <linux/list.h>
16#include <linux/device.h>
17#include <linux/platform_device.h>
18#include <linux/pci.h>
Wolfram Sang1e9d4212018-04-19 22:00:07 +020019#include <linux/platform_data/i2c-gpio.h>
Linus Walleijf68c0a82019-08-20 12:37:15 +020020#include <linux/gpio/driver.h>
Linus Walleijb2e63552017-09-10 01:30:46 +020021#include <linux/gpio/machine.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ben Dooksb6d64542007-02-20 13:58:01 -080023
24#include <linux/sm501.h>
25#include <linux/sm501-regs.h>
Magnus Damm61711f82008-04-28 02:14:22 -070026#include <linux/serial_8250.h>
Ben Dooksb6d64542007-02-20 13:58:01 -080027
Axel Linf77401d2010-11-10 15:47:51 +080028#include <linux/io.h>
Ben Dooksb6d64542007-02-20 13:58:01 -080029
30struct sm501_device {
31 struct list_head list;
32 struct platform_device pdev;
33};
34
Ben Dooksf61be272008-07-25 01:45:59 -070035struct sm501_gpio;
36
Ben Dooksf2999202008-07-25 01:46:02 -070037#ifdef CONFIG_MFD_SM501_GPIO
38#include <linux/gpio.h>
39
Ben Dooksf61be272008-07-25 01:45:59 -070040struct sm501_gpio_chip {
41 struct gpio_chip gpio;
42 struct sm501_gpio *ourgpio; /* to get back to parent. */
43 void __iomem *regbase;
Ben Dooks98325f82009-01-10 16:59:53 +010044 void __iomem *control; /* address of control reg. */
Ben Dooksf61be272008-07-25 01:45:59 -070045};
46
47struct sm501_gpio {
48 struct sm501_gpio_chip low;
49 struct sm501_gpio_chip high;
50 spinlock_t lock;
51
52 unsigned int registered : 1;
53 void __iomem *regs;
54 struct resource *regs_res;
55};
Ben Dooksf2999202008-07-25 01:46:02 -070056#else
57struct sm501_gpio {
58 /* no gpio support, empty definition for sm501_devdata. */
59};
60#endif
Ben Dooksf61be272008-07-25 01:45:59 -070061
Ben Dooksb6d64542007-02-20 13:58:01 -080062struct sm501_devdata {
63 spinlock_t reg_lock;
64 struct mutex clock_lock;
65 struct list_head devices;
Ben Dooksf61be272008-07-25 01:45:59 -070066 struct sm501_gpio gpio;
Ben Dooksb6d64542007-02-20 13:58:01 -080067
68 struct device *dev;
69 struct resource *io_res;
70 struct resource *mem_res;
71 struct resource *regs_claim;
72 struct sm501_platdata *platdata;
73
Ben Dooksf61be272008-07-25 01:45:59 -070074
Ben Dooks331d7472007-06-23 17:16:28 -070075 unsigned int in_suspend;
76 unsigned long pm_misc;
77
Ben Dooksb6d64542007-02-20 13:58:01 -080078 int unit_power[20];
79 unsigned int pdev_id;
80 unsigned int irq;
81 void __iomem *regs;
Ville Syrjala3149be52008-03-04 14:28:50 -080082 unsigned int rev;
Ben Dooksb6d64542007-02-20 13:58:01 -080083};
84
Ben Dooksf61be272008-07-25 01:45:59 -070085
Ben Dooksb6d64542007-02-20 13:58:01 -080086#define MHZ (1000 * 1000)
87
88#ifdef DEBUG
Ville Syrjala245904a2008-03-04 14:28:49 -080089static const unsigned int div_tab[] = {
Ben Dooksb6d64542007-02-20 13:58:01 -080090 [0] = 1,
91 [1] = 2,
92 [2] = 4,
93 [3] = 8,
94 [4] = 16,
95 [5] = 32,
96 [6] = 64,
97 [7] = 128,
98 [8] = 3,
99 [9] = 6,
100 [10] = 12,
101 [11] = 24,
102 [12] = 48,
103 [13] = 96,
104 [14] = 192,
105 [15] = 384,
106 [16] = 5,
107 [17] = 10,
108 [18] = 20,
109 [19] = 40,
110 [20] = 80,
111 [21] = 160,
112 [22] = 320,
113 [23] = 604,
114};
115
116static unsigned long decode_div(unsigned long pll2, unsigned long val,
117 unsigned int lshft, unsigned int selbit,
Ville Syrjala245904a2008-03-04 14:28:49 -0800118 unsigned long mask)
Ben Dooksb6d64542007-02-20 13:58:01 -0800119{
120 if (val & selbit)
121 pll2 = 288 * MHZ;
122
Ville Syrjala245904a2008-03-04 14:28:49 -0800123 return pll2 / div_tab[(val >> lshft) & mask];
Ben Dooksb6d64542007-02-20 13:58:01 -0800124}
125
126#define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
127
128/* sm501_dump_clk
129 *
130 * Print out the current clock configuration for the device
131*/
132
133static void sm501_dump_clk(struct sm501_devdata *sm)
134{
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000135 unsigned long misct = smc501_readl(sm->regs + SM501_MISC_TIMING);
136 unsigned long pm0 = smc501_readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
137 unsigned long pm1 = smc501_readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
138 unsigned long pmc = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800139 unsigned long sdclk0, sdclk1;
140 unsigned long pll2 = 0;
141
142 switch (misct & 0x30) {
143 case 0x00:
144 pll2 = 336 * MHZ;
145 break;
146 case 0x10:
147 pll2 = 288 * MHZ;
148 break;
149 case 0x20:
150 pll2 = 240 * MHZ;
151 break;
152 case 0x30:
153 pll2 = 192 * MHZ;
154 break;
155 }
156
157 sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
Ville Syrjala245904a2008-03-04 14:28:49 -0800158 sdclk0 /= div_tab[((misct >> 8) & 0xf)];
Ben Dooksb6d64542007-02-20 13:58:01 -0800159
160 sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
Ville Syrjala245904a2008-03-04 14:28:49 -0800161 sdclk1 /= div_tab[((misct >> 16) & 0xf)];
Ben Dooksb6d64542007-02-20 13:58:01 -0800162
163 dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
164 misct, pm0, pm1);
165
166 dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
167 fmt_freq(pll2), sdclk0, sdclk1);
168
169 dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
170
171 dev_dbg(sm->dev, "PM0[%c]: "
172 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
Ben Dooks48986f02007-11-28 16:21:37 -0800173 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
Ben Dooksb6d64542007-02-20 13:58:01 -0800174 (pmc & 3 ) == 0 ? '*' : '-',
Ville Syrjala245904a2008-03-04 14:28:49 -0800175 fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
176 fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
177 fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15)),
178 fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15)));
Ben Dooksb6d64542007-02-20 13:58:01 -0800179
180 dev_dbg(sm->dev, "PM1[%c]: "
181 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
182 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
183 (pmc & 3 ) == 1 ? '*' : '-',
Ville Syrjala245904a2008-03-04 14:28:49 -0800184 fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
185 fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
186 fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15)),
187 fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15)));
Ben Dooksb6d64542007-02-20 13:58:01 -0800188}
Ben Dooks331d7472007-06-23 17:16:28 -0700189
190static void sm501_dump_regs(struct sm501_devdata *sm)
Ben Dooksb6d64542007-02-20 13:58:01 -0800191{
Ben Dooks331d7472007-06-23 17:16:28 -0700192 void __iomem *regs = sm->regs;
193
194 dev_info(sm->dev, "System Control %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000195 smc501_readl(regs + SM501_SYSTEM_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700196 dev_info(sm->dev, "Misc Control %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000197 smc501_readl(regs + SM501_MISC_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700198 dev_info(sm->dev, "GPIO Control Low %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000199 smc501_readl(regs + SM501_GPIO31_0_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700200 dev_info(sm->dev, "GPIO Control Hi %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000201 smc501_readl(regs + SM501_GPIO63_32_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700202 dev_info(sm->dev, "DRAM Control %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000203 smc501_readl(regs + SM501_DRAM_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700204 dev_info(sm->dev, "Arbitration Ctrl %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000205 smc501_readl(regs + SM501_ARBTRTN_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700206 dev_info(sm->dev, "Misc Timing %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000207 smc501_readl(regs + SM501_MISC_TIMING));
Ben Dooksb6d64542007-02-20 13:58:01 -0800208}
Ben Dooks331d7472007-06-23 17:16:28 -0700209
210static void sm501_dump_gate(struct sm501_devdata *sm)
211{
212 dev_info(sm->dev, "CurrentGate %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000213 smc501_readl(sm->regs + SM501_CURRENT_GATE));
Ben Dooks331d7472007-06-23 17:16:28 -0700214 dev_info(sm->dev, "CurrentClock %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000215 smc501_readl(sm->regs + SM501_CURRENT_CLOCK));
Ben Dooks331d7472007-06-23 17:16:28 -0700216 dev_info(sm->dev, "PowerModeControl %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000217 smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700218}
219
220#else
221static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
222static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
223static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
Ben Dooksb6d64542007-02-20 13:58:01 -0800224#endif
225
226/* sm501_sync_regs
227 *
228 * ensure the
229*/
230
231static void sm501_sync_regs(struct sm501_devdata *sm)
232{
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000233 smc501_readl(sm->regs);
Ben Dooksb6d64542007-02-20 13:58:01 -0800234}
235
Ben Dooks331d7472007-06-23 17:16:28 -0700236static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
237{
238 /* during suspend/resume, we are currently not allowed to sleep,
239 * so change to using mdelay() instead of msleep() if we
240 * are in one of these paths */
241
242 if (sm->in_suspend)
243 mdelay(delay);
244 else
245 msleep(delay);
246}
247
Ben Dooksb6d64542007-02-20 13:58:01 -0800248/* sm501_misc_control
249 *
Ben Dooks331d7472007-06-23 17:16:28 -0700250 * alters the miscellaneous control parameters
Ben Dooksb6d64542007-02-20 13:58:01 -0800251*/
252
253int sm501_misc_control(struct device *dev,
254 unsigned long set, unsigned long clear)
255{
256 struct sm501_devdata *sm = dev_get_drvdata(dev);
257 unsigned long misc;
258 unsigned long save;
259 unsigned long to;
260
261 spin_lock_irqsave(&sm->reg_lock, save);
262
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000263 misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800264 to = (misc & ~clear) | set;
265
266 if (to != misc) {
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000267 smc501_writel(to, sm->regs + SM501_MISC_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800268 sm501_sync_regs(sm);
269
270 dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
271 }
272
273 spin_unlock_irqrestore(&sm->reg_lock, save);
274 return to;
275}
276
277EXPORT_SYMBOL_GPL(sm501_misc_control);
278
279/* sm501_modify_reg
280 *
281 * Modify a register in the SM501 which may be shared with other
282 * drivers.
283*/
284
285unsigned long sm501_modify_reg(struct device *dev,
286 unsigned long reg,
287 unsigned long set,
288 unsigned long clear)
289{
290 struct sm501_devdata *sm = dev_get_drvdata(dev);
291 unsigned long data;
292 unsigned long save;
293
294 spin_lock_irqsave(&sm->reg_lock, save);
295
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000296 data = smc501_readl(sm->regs + reg);
Ben Dooksb6d64542007-02-20 13:58:01 -0800297 data |= set;
298 data &= ~clear;
299
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000300 smc501_writel(data, sm->regs + reg);
Ben Dooksb6d64542007-02-20 13:58:01 -0800301 sm501_sync_regs(sm);
302
303 spin_unlock_irqrestore(&sm->reg_lock, save);
304
305 return data;
306}
307
308EXPORT_SYMBOL_GPL(sm501_modify_reg);
309
Ben Dooksb6d64542007-02-20 13:58:01 -0800310/* sm501_unit_power
311 *
312 * alters the power active gate to set specific units on or off
313 */
314
315int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
316{
317 struct sm501_devdata *sm = dev_get_drvdata(dev);
318 unsigned long mode;
319 unsigned long gate;
320 unsigned long clock;
321
322 mutex_lock(&sm->clock_lock);
323
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000324 mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
325 gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
326 clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800327
328 mode &= 3; /* get current power mode */
329
Adrian Bunkbf703c32007-04-01 23:49:38 -0700330 if (unit >= ARRAY_SIZE(sm->unit_power)) {
Harvey Harrison145980a2008-04-30 00:54:57 -0700331 dev_err(dev, "%s: bad unit %d\n", __func__, unit);
Ben Dooksb6d64542007-02-20 13:58:01 -0800332 goto already;
333 }
334
Harvey Harrison145980a2008-04-30 00:54:57 -0700335 dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __func__, unit,
Ben Dooksb6d64542007-02-20 13:58:01 -0800336 sm->unit_power[unit], to);
337
338 if (to == 0 && sm->unit_power[unit] == 0) {
339 dev_err(sm->dev, "unit %d is already shutdown\n", unit);
340 goto already;
341 }
342
343 sm->unit_power[unit] += to ? 1 : -1;
344 to = sm->unit_power[unit] ? 1 : 0;
345
346 if (to) {
347 if (gate & (1 << unit))
348 goto already;
349 gate |= (1 << unit);
350 } else {
351 if (!(gate & (1 << unit)))
352 goto already;
353 gate &= ~(1 << unit);
354 }
355
356 switch (mode) {
357 case 1:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000358 smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
359 smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800360 mode = 0;
361 break;
362 case 2:
363 case 0:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000364 smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
365 smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800366 mode = 1;
367 break;
368
369 default:
Jiri Slaby992bb252009-06-22 00:00:02 +0200370 gate = -1;
371 goto already;
Ben Dooksb6d64542007-02-20 13:58:01 -0800372 }
373
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000374 smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800375 sm501_sync_regs(sm);
376
377 dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
378 gate, clock, mode);
379
Ben Dooks331d7472007-06-23 17:16:28 -0700380 sm501_mdelay(sm, 16);
Ben Dooksb6d64542007-02-20 13:58:01 -0800381
382 already:
383 mutex_unlock(&sm->clock_lock);
384 return gate;
385}
386
387EXPORT_SYMBOL_GPL(sm501_unit_power);
388
Ben Dooksb6d64542007-02-20 13:58:01 -0800389/* clock value structure. */
390struct sm501_clock {
391 unsigned long mclk;
392 int divider;
393 int shift;
Ville Syrjala3149be52008-03-04 14:28:50 -0800394 unsigned int m, n, k;
Ben Dooksb6d64542007-02-20 13:58:01 -0800395};
396
Ville Syrjala3149be52008-03-04 14:28:50 -0800397/* sm501_calc_clock
398 *
399 * Calculates the nearest discrete clock frequency that
400 * can be achieved with the specified input clock.
401 * the maximum divisor is 3 or 5
402 */
403
404static int sm501_calc_clock(unsigned long freq,
405 struct sm501_clock *clock,
406 int max_div,
407 unsigned long mclk,
408 long *best_diff)
409{
410 int ret = 0;
411 int divider;
412 int shift;
413 long diff;
414
415 /* try dividers 1 and 3 for CRT and for panel,
416 try divider 5 for panel only.*/
417
418 for (divider = 1; divider <= max_div; divider += 2) {
419 /* try all 8 shift values.*/
420 for (shift = 0; shift < 8; shift++) {
421 /* Calculate difference to requested clock */
Axel Lin829ecbc2012-03-12 10:12:34 +0800422 diff = DIV_ROUND_CLOSEST(mclk, divider << shift) - freq;
Ville Syrjala3149be52008-03-04 14:28:50 -0800423 if (diff < 0)
424 diff = -diff;
425
426 /* If it is less than the current, use it */
427 if (diff < *best_diff) {
428 *best_diff = diff;
429
430 clock->mclk = mclk;
431 clock->divider = divider;
432 clock->shift = shift;
433 ret = 1;
434 }
435 }
436 }
437
438 return ret;
439}
440
441/* sm501_calc_pll
442 *
443 * Calculates the nearest discrete clock frequency that can be
444 * achieved using the programmable PLL.
445 * the maximum divisor is 3 or 5
446 */
447
448static unsigned long sm501_calc_pll(unsigned long freq,
449 struct sm501_clock *clock,
450 int max_div)
451{
452 unsigned long mclk;
453 unsigned int m, n, k;
454 long best_diff = 999999999;
455
456 /*
457 * The SM502 datasheet doesn't specify the min/max values for M and N.
458 * N = 1 at least doesn't work in practice.
459 */
460 for (m = 2; m <= 255; m++) {
461 for (n = 2; n <= 127; n++) {
462 for (k = 0; k <= 1; k++) {
463 mclk = (24000000UL * m / n) >> k;
464
465 if (sm501_calc_clock(freq, clock, max_div,
466 mclk, &best_diff)) {
467 clock->m = m;
468 clock->n = n;
469 clock->k = k;
470 }
471 }
472 }
473 }
474
475 /* Return best clock. */
476 return clock->mclk / (clock->divider << clock->shift);
477}
478
Ben Dooksb6d64542007-02-20 13:58:01 -0800479/* sm501_select_clock
480 *
Ville Syrjala3149be52008-03-04 14:28:50 -0800481 * Calculates the nearest discrete clock frequency that can be
482 * achieved using the 288MHz and 336MHz PLLs.
Ben Dooksb6d64542007-02-20 13:58:01 -0800483 * the maximum divisor is 3 or 5
484 */
Ville Syrjala3149be52008-03-04 14:28:50 -0800485
Ben Dooksb6d64542007-02-20 13:58:01 -0800486static unsigned long sm501_select_clock(unsigned long freq,
487 struct sm501_clock *clock,
488 int max_div)
489{
490 unsigned long mclk;
Ben Dooksb6d64542007-02-20 13:58:01 -0800491 long best_diff = 999999999;
492
493 /* Try 288MHz and 336MHz clocks. */
494 for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
Ville Syrjala3149be52008-03-04 14:28:50 -0800495 sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
Ben Dooksb6d64542007-02-20 13:58:01 -0800496 }
497
498 /* Return best clock. */
499 return clock->mclk / (clock->divider << clock->shift);
500}
501
502/* sm501_set_clock
503 *
504 * set one of the four clock sources to the closest available frequency to
505 * the one specified
506*/
507
508unsigned long sm501_set_clock(struct device *dev,
509 int clksrc,
510 unsigned long req_freq)
511{
512 struct sm501_devdata *sm = dev_get_drvdata(dev);
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000513 unsigned long mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
514 unsigned long gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
515 unsigned long clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ville Syrjala3149be52008-03-04 14:28:50 -0800516 unsigned int pll_reg = 0;
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800517 unsigned long sm501_freq; /* the actual frequency achieved */
Lee Jones5f114eb2014-08-18 16:11:27 +0100518 u64 reg;
Ben Dooksb6d64542007-02-20 13:58:01 -0800519
520 struct sm501_clock to;
521
522 /* find achivable discrete frequency and setup register value
523 * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
524 * has an extra bit for the divider */
525
526 switch (clksrc) {
527 case SM501_CLOCK_P2XCLK:
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800528 /* This clock is divided in half so to achieve the
Ben Dooksb6d64542007-02-20 13:58:01 -0800529 * requested frequency the value must be multiplied by
530 * 2. This clock also has an additional pre divisor */
531
Ville Syrjala3149be52008-03-04 14:28:50 -0800532 if (sm->rev >= 0xC0) {
533 /* SM502 -> use the programmable PLL */
534 sm501_freq = (sm501_calc_pll(2 * req_freq,
535 &to, 5) / 2);
536 reg = to.shift & 0x07;/* bottom 3 bits are shift */
537 if (to.divider == 3)
538 reg |= 0x08; /* /3 divider required */
539 else if (to.divider == 5)
540 reg |= 0x10; /* /5 divider required */
541 reg |= 0x40; /* select the programmable PLL */
542 pll_reg = 0x20000 | (to.k << 15) | (to.n << 8) | to.m;
543 } else {
544 sm501_freq = (sm501_select_clock(2 * req_freq,
545 &to, 5) / 2);
546 reg = to.shift & 0x07;/* bottom 3 bits are shift */
547 if (to.divider == 3)
548 reg |= 0x08; /* /3 divider required */
549 else if (to.divider == 5)
550 reg |= 0x10; /* /5 divider required */
551 if (to.mclk != 288000000)
552 reg |= 0x20; /* which mclk pll is source */
553 }
Ben Dooksb6d64542007-02-20 13:58:01 -0800554 break;
555
556 case SM501_CLOCK_V2XCLK:
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800557 /* This clock is divided in half so to achieve the
Ben Dooksb6d64542007-02-20 13:58:01 -0800558 * requested frequency the value must be multiplied by 2. */
559
560 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
561 reg=to.shift & 0x07; /* bottom 3 bits are shift */
562 if (to.divider == 3)
563 reg |= 0x08; /* /3 divider required */
564 if (to.mclk != 288000000)
565 reg |= 0x10; /* which mclk pll is source */
566 break;
567
568 case SM501_CLOCK_MCLK:
569 case SM501_CLOCK_M1XCLK:
570 /* These clocks are the same and not further divided */
571
572 sm501_freq = sm501_select_clock( req_freq, &to, 3);
573 reg=to.shift & 0x07; /* bottom 3 bits are shift */
574 if (to.divider == 3)
575 reg |= 0x08; /* /3 divider required */
576 if (to.mclk != 288000000)
577 reg |= 0x10; /* which mclk pll is source */
578 break;
579
580 default:
581 return 0; /* this is bad */
582 }
583
584 mutex_lock(&sm->clock_lock);
585
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000586 mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
587 gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
588 clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800589
590 clock = clock & ~(0xFF << clksrc);
591 clock |= reg<<clksrc;
592
593 mode &= 3; /* find current mode */
594
595 switch (mode) {
596 case 1:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000597 smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
598 smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800599 mode = 0;
600 break;
601 case 2:
602 case 0:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000603 smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
604 smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800605 mode = 1;
606 break;
607
608 default:
609 mutex_unlock(&sm->clock_lock);
610 return -1;
611 }
612
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000613 smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
Ville Syrjala3149be52008-03-04 14:28:50 -0800614
615 if (pll_reg)
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000616 smc501_writel(pll_reg,
617 sm->regs + SM501_PROGRAMMABLE_PLL_CONTROL);
Ville Syrjala3149be52008-03-04 14:28:50 -0800618
Ben Dooksb6d64542007-02-20 13:58:01 -0800619 sm501_sync_regs(sm);
620
Ben Dooks80e74a82008-08-10 23:45:24 +0200621 dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
622 gate, clock, mode);
Ben Dooksb6d64542007-02-20 13:58:01 -0800623
Ben Dooks331d7472007-06-23 17:16:28 -0700624 sm501_mdelay(sm, 16);
Ben Dooksb6d64542007-02-20 13:58:01 -0800625 mutex_unlock(&sm->clock_lock);
626
627 sm501_dump_clk(sm);
628
629 return sm501_freq;
630}
631
632EXPORT_SYMBOL_GPL(sm501_set_clock);
633
634/* sm501_find_clock
635 *
636 * finds the closest available frequency for a given clock
637*/
638
Ville Syrjala3149be52008-03-04 14:28:50 -0800639unsigned long sm501_find_clock(struct device *dev,
640 int clksrc,
Ben Dooksb6d64542007-02-20 13:58:01 -0800641 unsigned long req_freq)
642{
Ville Syrjala3149be52008-03-04 14:28:50 -0800643 struct sm501_devdata *sm = dev_get_drvdata(dev);
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800644 unsigned long sm501_freq; /* the frequency achieveable by the 501 */
Ben Dooksb6d64542007-02-20 13:58:01 -0800645 struct sm501_clock to;
646
647 switch (clksrc) {
648 case SM501_CLOCK_P2XCLK:
Ville Syrjala3149be52008-03-04 14:28:50 -0800649 if (sm->rev >= 0xC0) {
650 /* SM502 -> use the programmable PLL */
651 sm501_freq = (sm501_calc_pll(2 * req_freq,
652 &to, 5) / 2);
653 } else {
654 sm501_freq = (sm501_select_clock(2 * req_freq,
655 &to, 5) / 2);
656 }
Ben Dooksb6d64542007-02-20 13:58:01 -0800657 break;
658
659 case SM501_CLOCK_V2XCLK:
660 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
661 break;
662
663 case SM501_CLOCK_MCLK:
664 case SM501_CLOCK_M1XCLK:
665 sm501_freq = sm501_select_clock(req_freq, &to, 3);
666 break;
667
668 default:
669 sm501_freq = 0; /* error */
670 }
671
672 return sm501_freq;
673}
674
675EXPORT_SYMBOL_GPL(sm501_find_clock);
676
677static struct sm501_device *to_sm_device(struct platform_device *pdev)
678{
679 return container_of(pdev, struct sm501_device, pdev);
680}
681
682/* sm501_device_release
683 *
684 * A release function for the platform devices we create to allow us to
685 * free any items we allocated
686*/
687
688static void sm501_device_release(struct device *dev)
689{
690 kfree(to_sm_device(to_platform_device(dev)));
691}
692
693/* sm501_create_subdev
694 *
695 * Create a skeleton platform device with resources for passing to a
696 * sub-driver
697*/
698
699static struct platform_device *
Magnus Damm61711f82008-04-28 02:14:22 -0700700sm501_create_subdev(struct sm501_devdata *sm, char *name,
701 unsigned int res_count, unsigned int platform_data_size)
Ben Dooksb6d64542007-02-20 13:58:01 -0800702{
703 struct sm501_device *smdev;
704
705 smdev = kzalloc(sizeof(struct sm501_device) +
Magnus Damm61711f82008-04-28 02:14:22 -0700706 (sizeof(struct resource) * res_count) +
707 platform_data_size, GFP_KERNEL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800708 if (!smdev)
709 return NULL;
710
711 smdev->pdev.dev.release = sm501_device_release;
712
713 smdev->pdev.name = name;
714 smdev->pdev.id = sm->pdev_id;
Ben Dooksb6d64542007-02-20 13:58:01 -0800715 smdev->pdev.dev.parent = sm->dev;
Guenter Roeck2f606da2018-08-03 20:59:51 -0700716 smdev->pdev.dev.coherent_dma_mask = 0xffffffff;
Ben Dooksb6d64542007-02-20 13:58:01 -0800717
Magnus Damm61711f82008-04-28 02:14:22 -0700718 if (res_count) {
719 smdev->pdev.resource = (struct resource *)(smdev+1);
720 smdev->pdev.num_resources = res_count;
721 }
722 if (platform_data_size)
723 smdev->pdev.dev.platform_data = (void *)(smdev+1);
724
Ben Dooksb6d64542007-02-20 13:58:01 -0800725 return &smdev->pdev;
726}
727
728/* sm501_register_device
729 *
730 * Register a platform device created with sm501_create_subdev()
731*/
732
733static int sm501_register_device(struct sm501_devdata *sm,
734 struct platform_device *pdev)
735{
736 struct sm501_device *smdev = to_sm_device(pdev);
737 int ptr;
738 int ret;
739
740 for (ptr = 0; ptr < pdev->num_resources; ptr++) {
Joe Perches3f3d4310b2010-11-12 13:37:56 -0800741 printk(KERN_DEBUG "%s[%d] %pR\n",
742 pdev->name, ptr, &pdev->resource[ptr]);
Ben Dooksb6d64542007-02-20 13:58:01 -0800743 }
744
745 ret = platform_device_register(pdev);
746
747 if (ret >= 0) {
748 dev_dbg(sm->dev, "registered %s\n", pdev->name);
749 list_add_tail(&smdev->list, &sm->devices);
750 } else
751 dev_err(sm->dev, "error registering %s (%d)\n",
752 pdev->name, ret);
753
754 return ret;
755}
756
757/* sm501_create_subio
758 *
759 * Fill in an IO resource for a sub device
760*/
761
762static void sm501_create_subio(struct sm501_devdata *sm,
763 struct resource *res,
764 resource_size_t offs,
765 resource_size_t size)
766{
767 res->flags = IORESOURCE_MEM;
768 res->parent = sm->io_res;
769 res->start = sm->io_res->start + offs;
770 res->end = res->start + size - 1;
771}
772
773/* sm501_create_mem
774 *
775 * Fill in an MEM resource for a sub device
776*/
777
778static void sm501_create_mem(struct sm501_devdata *sm,
779 struct resource *res,
780 resource_size_t *offs,
781 resource_size_t size)
782{
783 *offs -= size; /* adjust memory size */
784
785 res->flags = IORESOURCE_MEM;
786 res->parent = sm->mem_res;
787 res->start = sm->mem_res->start + *offs;
788 res->end = res->start + size - 1;
789}
790
791/* sm501_create_irq
792 *
793 * Fill in an IRQ resource for a sub device
794*/
795
796static void sm501_create_irq(struct sm501_devdata *sm,
797 struct resource *res)
798{
799 res->flags = IORESOURCE_IRQ;
800 res->parent = NULL;
801 res->start = res->end = sm->irq;
802}
803
804static int sm501_register_usbhost(struct sm501_devdata *sm,
805 resource_size_t *mem_avail)
806{
807 struct platform_device *pdev;
808
Magnus Damm61711f82008-04-28 02:14:22 -0700809 pdev = sm501_create_subdev(sm, "sm501-usb", 3, 0);
Ben Dooksb6d64542007-02-20 13:58:01 -0800810 if (!pdev)
811 return -ENOMEM;
812
813 sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
814 sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
815 sm501_create_irq(sm, &pdev->resource[2]);
816
817 return sm501_register_device(sm, pdev);
818}
819
Magnus Damm61711f82008-04-28 02:14:22 -0700820static void sm501_setup_uart_data(struct sm501_devdata *sm,
821 struct plat_serial8250_port *uart_data,
822 unsigned int offset)
823{
824 uart_data->membase = sm->regs + offset;
825 uart_data->mapbase = sm->io_res->start + offset;
826 uart_data->iotype = UPIO_MEM;
827 uart_data->irq = sm->irq;
828 uart_data->flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
829 uart_data->regshift = 2;
830 uart_data->uartclk = (9600 * 16);
831}
832
833static int sm501_register_uart(struct sm501_devdata *sm, int devices)
834{
835 struct platform_device *pdev;
836 struct plat_serial8250_port *uart_data;
837
838 pdev = sm501_create_subdev(sm, "serial8250", 0,
839 sizeof(struct plat_serial8250_port) * 3);
840 if (!pdev)
841 return -ENOMEM;
842
Jingoo Han334a41ce2013-07-30 17:10:05 +0900843 uart_data = dev_get_platdata(&pdev->dev);
Magnus Damm61711f82008-04-28 02:14:22 -0700844
845 if (devices & SM501_USE_UART0) {
846 sm501_setup_uart_data(sm, uart_data++, 0x30000);
847 sm501_unit_power(sm->dev, SM501_GATE_UART0, 1);
848 sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 12, 0);
849 sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x01e0, 0);
850 }
851 if (devices & SM501_USE_UART1) {
852 sm501_setup_uart_data(sm, uart_data++, 0x30020);
853 sm501_unit_power(sm->dev, SM501_GATE_UART1, 1);
854 sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 13, 0);
855 sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x1e00, 0);
856 }
857
858 pdev->id = PLAT8250_DEV_SM501;
859
860 return sm501_register_device(sm, pdev);
861}
862
Ben Dooksb6d64542007-02-20 13:58:01 -0800863static int sm501_register_display(struct sm501_devdata *sm,
864 resource_size_t *mem_avail)
865{
866 struct platform_device *pdev;
867
Magnus Damm61711f82008-04-28 02:14:22 -0700868 pdev = sm501_create_subdev(sm, "sm501-fb", 4, 0);
Ben Dooksb6d64542007-02-20 13:58:01 -0800869 if (!pdev)
870 return -ENOMEM;
871
872 sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
873 sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
874 sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
875 sm501_create_irq(sm, &pdev->resource[3]);
876
877 return sm501_register_device(sm, pdev);
878}
879
Ben Dooksf61be272008-07-25 01:45:59 -0700880#ifdef CONFIG_MFD_SM501_GPIO
881
Ben Dooksf61be272008-07-25 01:45:59 -0700882static inline struct sm501_devdata *sm501_gpio_to_dev(struct sm501_gpio *gpio)
883{
884 return container_of(gpio, struct sm501_devdata, gpio);
885}
886
887static int sm501_gpio_get(struct gpio_chip *chip, unsigned offset)
888
889{
Linus Walleij3a504102016-03-30 10:48:05 +0200890 struct sm501_gpio_chip *smgpio = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700891 unsigned long result;
892
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000893 result = smc501_readl(smgpio->regbase + SM501_GPIO_DATA_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700894 result >>= offset;
895
896 return result & 1UL;
897}
898
Ben Dooks98325f82009-01-10 16:59:53 +0100899static void sm501_gpio_ensure_gpio(struct sm501_gpio_chip *smchip,
900 unsigned long bit)
901{
902 unsigned long ctrl;
903
904 /* check and modify if this pin is not set as gpio. */
905
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000906 if (smc501_readl(smchip->control) & bit) {
Ben Dooks98325f82009-01-10 16:59:53 +0100907 dev_info(sm501_gpio_to_dev(smchip->ourgpio)->dev,
908 "changing mode of gpio, bit %08lx\n", bit);
909
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000910 ctrl = smc501_readl(smchip->control);
Ben Dooks98325f82009-01-10 16:59:53 +0100911 ctrl &= ~bit;
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000912 smc501_writel(ctrl, smchip->control);
Ben Dooks98325f82009-01-10 16:59:53 +0100913
914 sm501_sync_regs(sm501_gpio_to_dev(smchip->ourgpio));
915 }
916}
917
Ben Dooksf61be272008-07-25 01:45:59 -0700918static void sm501_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
919
920{
Linus Walleij3a504102016-03-30 10:48:05 +0200921 struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700922 struct sm501_gpio *smgpio = smchip->ourgpio;
923 unsigned long bit = 1 << offset;
924 void __iomem *regs = smchip->regbase;
925 unsigned long save;
926 unsigned long val;
927
928 dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
929 __func__, chip, offset);
930
931 spin_lock_irqsave(&smgpio->lock, save);
932
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000933 val = smc501_readl(regs + SM501_GPIO_DATA_LOW) & ~bit;
Ben Dooksf61be272008-07-25 01:45:59 -0700934 if (value)
935 val |= bit;
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000936 smc501_writel(val, regs);
Ben Dooksf61be272008-07-25 01:45:59 -0700937
938 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
Ben Dooks98325f82009-01-10 16:59:53 +0100939 sm501_gpio_ensure_gpio(smchip, bit);
940
Ben Dooksf61be272008-07-25 01:45:59 -0700941 spin_unlock_irqrestore(&smgpio->lock, save);
942}
943
944static int sm501_gpio_input(struct gpio_chip *chip, unsigned offset)
945{
Linus Walleij3a504102016-03-30 10:48:05 +0200946 struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700947 struct sm501_gpio *smgpio = smchip->ourgpio;
948 void __iomem *regs = smchip->regbase;
949 unsigned long bit = 1 << offset;
950 unsigned long save;
951 unsigned long ddr;
952
Ben Dooks98325f82009-01-10 16:59:53 +0100953 dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
954 __func__, chip, offset);
Ben Dooksf61be272008-07-25 01:45:59 -0700955
956 spin_lock_irqsave(&smgpio->lock, save);
957
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000958 ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
959 smc501_writel(ddr & ~bit, regs + SM501_GPIO_DDR_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700960
961 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
Ben Dooks98325f82009-01-10 16:59:53 +0100962 sm501_gpio_ensure_gpio(smchip, bit);
963
Ben Dooksf61be272008-07-25 01:45:59 -0700964 spin_unlock_irqrestore(&smgpio->lock, save);
965
966 return 0;
967}
968
969static int sm501_gpio_output(struct gpio_chip *chip,
970 unsigned offset, int value)
971{
Linus Walleij3a504102016-03-30 10:48:05 +0200972 struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700973 struct sm501_gpio *smgpio = smchip->ourgpio;
974 unsigned long bit = 1 << offset;
975 void __iomem *regs = smchip->regbase;
976 unsigned long save;
977 unsigned long val;
978 unsigned long ddr;
979
980 dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d,%d)\n",
981 __func__, chip, offset, value);
982
983 spin_lock_irqsave(&smgpio->lock, save);
984
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000985 val = smc501_readl(regs + SM501_GPIO_DATA_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700986 if (value)
987 val |= bit;
988 else
989 val &= ~bit;
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000990 smc501_writel(val, regs);
Ben Dooksf61be272008-07-25 01:45:59 -0700991
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000992 ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
993 smc501_writel(ddr | bit, regs + SM501_GPIO_DDR_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700994
995 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000996 smc501_writel(val, regs + SM501_GPIO_DATA_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700997
998 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
999 spin_unlock_irqrestore(&smgpio->lock, save);
1000
1001 return 0;
1002}
1003
Julia Lawall7e94e512016-09-11 14:14:38 +02001004static const struct gpio_chip gpio_chip_template = {
Ben Dooksf61be272008-07-25 01:45:59 -07001005 .ngpio = 32,
1006 .direction_input = sm501_gpio_input,
1007 .direction_output = sm501_gpio_output,
1008 .set = sm501_gpio_set,
1009 .get = sm501_gpio_get,
1010};
1011
Bill Pembertonf791be42012-11-19 13:23:04 -05001012static int sm501_gpio_register_chip(struct sm501_devdata *sm,
Ben Dooksf61be272008-07-25 01:45:59 -07001013 struct sm501_gpio *gpio,
1014 struct sm501_gpio_chip *chip)
1015{
1016 struct sm501_platdata *pdata = sm->platdata;
1017 struct gpio_chip *gchip = &chip->gpio;
Arnaud Patard60e540d2008-07-25 01:46:00 -07001018 int base = pdata->gpio_base;
Ben Dooksf61be272008-07-25 01:45:59 -07001019
Ben Dooks28130be2008-07-25 01:46:02 -07001020 chip->gpio = gpio_chip_template;
Ben Dooksf61be272008-07-25 01:45:59 -07001021
1022 if (chip == &gpio->high) {
Arnaud Patard60e540d2008-07-25 01:46:00 -07001023 if (base > 0)
1024 base += 32;
Ben Dooksf61be272008-07-25 01:45:59 -07001025 chip->regbase = gpio->regs + SM501_GPIO_DATA_HIGH;
Ben Dooks98325f82009-01-10 16:59:53 +01001026 chip->control = sm->regs + SM501_GPIO63_32_CONTROL;
Ben Dooksf61be272008-07-25 01:45:59 -07001027 gchip->label = "SM501-HIGH";
1028 } else {
1029 chip->regbase = gpio->regs + SM501_GPIO_DATA_LOW;
Ben Dooks98325f82009-01-10 16:59:53 +01001030 chip->control = sm->regs + SM501_GPIO31_0_CONTROL;
Ben Dooksf61be272008-07-25 01:45:59 -07001031 gchip->label = "SM501-LOW";
1032 }
1033
1034 gchip->base = base;
1035 chip->ourgpio = gpio;
1036
Linus Walleij3a504102016-03-30 10:48:05 +02001037 return gpiochip_add_data(gchip, chip);
Ben Dooksf61be272008-07-25 01:45:59 -07001038}
1039
Bill Pembertonf791be42012-11-19 13:23:04 -05001040static int sm501_register_gpio(struct sm501_devdata *sm)
Ben Dooksf61be272008-07-25 01:45:59 -07001041{
1042 struct sm501_gpio *gpio = &sm->gpio;
1043 resource_size_t iobase = sm->io_res->start + SM501_GPIO;
1044 int ret;
Ben Dooksf61be272008-07-25 01:45:59 -07001045
1046 dev_dbg(sm->dev, "registering gpio block %08llx\n",
1047 (unsigned long long)iobase);
1048
1049 spin_lock_init(&gpio->lock);
1050
1051 gpio->regs_res = request_mem_region(iobase, 0x20, "sm501-gpio");
Markus Elfring42021512018-03-08 14:20:06 +01001052 if (!gpio->regs_res) {
Ben Dooksf61be272008-07-25 01:45:59 -07001053 dev_err(sm->dev, "gpio: failed to request region\n");
1054 return -ENXIO;
1055 }
1056
1057 gpio->regs = ioremap(iobase, 0x20);
Markus Elfring42021512018-03-08 14:20:06 +01001058 if (!gpio->regs) {
Ben Dooksf61be272008-07-25 01:45:59 -07001059 dev_err(sm->dev, "gpio: failed to remap registers\n");
1060 ret = -ENXIO;
Ben Dooks28130be2008-07-25 01:46:02 -07001061 goto err_claimed;
Ben Dooksf61be272008-07-25 01:45:59 -07001062 }
1063
1064 /* Register both our chips. */
1065
1066 ret = sm501_gpio_register_chip(sm, gpio, &gpio->low);
1067 if (ret) {
1068 dev_err(sm->dev, "failed to add low chip\n");
1069 goto err_mapped;
1070 }
1071
1072 ret = sm501_gpio_register_chip(sm, gpio, &gpio->high);
1073 if (ret) {
1074 dev_err(sm->dev, "failed to add high chip\n");
1075 goto err_low_chip;
1076 }
1077
1078 gpio->registered = 1;
1079
1080 return 0;
1081
1082 err_low_chip:
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001083 gpiochip_remove(&gpio->low.gpio);
Ben Dooksf61be272008-07-25 01:45:59 -07001084
1085 err_mapped:
Ben Dooks28130be2008-07-25 01:46:02 -07001086 iounmap(gpio->regs);
1087
1088 err_claimed:
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001089 release_mem_region(iobase, 0x20);
Ben Dooksf61be272008-07-25 01:45:59 -07001090
1091 return ret;
1092}
1093
1094static void sm501_gpio_remove(struct sm501_devdata *sm)
1095{
Ben Dooks28130be2008-07-25 01:46:02 -07001096 struct sm501_gpio *gpio = &sm->gpio;
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001097 resource_size_t iobase = sm->io_res->start + SM501_GPIO;
Ben Dooksf61be272008-07-25 01:45:59 -07001098
Ben Dooksf2999202008-07-25 01:46:02 -07001099 if (!sm->gpio.registered)
1100 return;
1101
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001102 gpiochip_remove(&gpio->low.gpio);
1103 gpiochip_remove(&gpio->high.gpio);
Ben Dooks28130be2008-07-25 01:46:02 -07001104
1105 iounmap(gpio->regs);
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001106 release_mem_region(iobase, 0x20);
Ben Dooksf61be272008-07-25 01:45:59 -07001107}
1108
Ben Dooksf2999202008-07-25 01:46:02 -07001109static inline int sm501_gpio_isregistered(struct sm501_devdata *sm)
1110{
1111 return sm->gpio.registered;
1112}
Ben Dooksf61be272008-07-25 01:45:59 -07001113#else
Ben Dooks28130be2008-07-25 01:46:02 -07001114static inline int sm501_register_gpio(struct sm501_devdata *sm)
Ben Dooksf61be272008-07-25 01:45:59 -07001115{
1116 return 0;
1117}
1118
Ben Dooks28130be2008-07-25 01:46:02 -07001119static inline void sm501_gpio_remove(struct sm501_devdata *sm)
Ben Dooksf61be272008-07-25 01:45:59 -07001120{
1121}
Ben Dooks42cd2362008-07-25 01:46:01 -07001122
Ben Dooksf2999202008-07-25 01:46:02 -07001123static inline int sm501_gpio_isregistered(struct sm501_devdata *sm)
1124{
1125 return 0;
1126}
Ben Dooksf61be272008-07-25 01:45:59 -07001127#endif
1128
Ben Dooks42cd2362008-07-25 01:46:01 -07001129static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm,
1130 struct sm501_platdata_gpio_i2c *iic)
1131{
1132 struct i2c_gpio_platform_data *icd;
1133 struct platform_device *pdev;
Linus Walleijb2e63552017-09-10 01:30:46 +02001134 struct gpiod_lookup_table *lookup;
Ben Dooks42cd2362008-07-25 01:46:01 -07001135
1136 pdev = sm501_create_subdev(sm, "i2c-gpio", 0,
1137 sizeof(struct i2c_gpio_platform_data));
1138 if (!pdev)
1139 return -ENOMEM;
1140
Linus Walleijb2e63552017-09-10 01:30:46 +02001141 /* Create a gpiod lookup using gpiochip-local offsets */
Gustavo A. R. Silva9f208ec2019-01-22 10:58:20 -06001142 lookup = devm_kzalloc(&pdev->dev, struct_size(lookup, table, 3),
Linus Walleijb2e63552017-09-10 01:30:46 +02001143 GFP_KERNEL);
Gustavo A. R. Silvaae7b8ed2019-01-22 10:56:36 -06001144 if (!lookup)
1145 return -ENOMEM;
1146
Linus Walleijb2e63552017-09-10 01:30:46 +02001147 lookup->dev_id = "i2c-gpio";
1148 if (iic->pin_sda < 32)
1149 lookup->table[0].chip_label = "SM501-LOW";
1150 else
1151 lookup->table[0].chip_label = "SM501-HIGH";
1152 lookup->table[0].chip_hwnum = iic->pin_sda % 32;
1153 lookup->table[0].con_id = NULL;
1154 lookup->table[0].idx = 0;
Linus Walleij4d0ce622017-09-10 23:03:32 +02001155 lookup->table[0].flags = GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN;
Linus Walleijb2e63552017-09-10 01:30:46 +02001156 if (iic->pin_scl < 32)
1157 lookup->table[1].chip_label = "SM501-LOW";
1158 else
1159 lookup->table[1].chip_label = "SM501-HIGH";
1160 lookup->table[1].chip_hwnum = iic->pin_scl % 32;
1161 lookup->table[1].con_id = NULL;
1162 lookup->table[1].idx = 1;
Linus Walleij4d0ce622017-09-10 23:03:32 +02001163 lookup->table[1].flags = GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN;
Linus Walleijb2e63552017-09-10 01:30:46 +02001164 gpiod_add_lookup_table(lookup);
1165
Jingoo Han334a41ce2013-07-30 17:10:05 +09001166 icd = dev_get_platdata(&pdev->dev);
Ben Dooks42cd2362008-07-25 01:46:01 -07001167 icd->timeout = iic->timeout;
1168 icd->udelay = iic->udelay;
1169
1170 /* note, we can't use either of the pin numbers, as the i2c-gpio
1171 * driver uses the platform.id field to generate the bus number
1172 * to register with the i2c core; The i2c core doesn't have enough
1173 * entries to deal with anything we currently use.
1174 */
1175
1176 pdev->id = iic->bus_num;
1177
Linus Walleijb2e63552017-09-10 01:30:46 +02001178 dev_info(sm->dev, "registering i2c-%d: sda=%d, scl=%d\n",
Ben Dooks42cd2362008-07-25 01:46:01 -07001179 iic->bus_num,
Linus Walleijb2e63552017-09-10 01:30:46 +02001180 iic->pin_sda, iic->pin_scl);
Ben Dooks42cd2362008-07-25 01:46:01 -07001181
1182 return sm501_register_device(sm, pdev);
1183}
1184
1185static int sm501_register_gpio_i2c(struct sm501_devdata *sm,
1186 struct sm501_platdata *pdata)
1187{
1188 struct sm501_platdata_gpio_i2c *iic = pdata->gpio_i2c;
1189 int index;
1190 int ret;
1191
1192 for (index = 0; index < pdata->gpio_i2c_nr; index++, iic++) {
1193 ret = sm501_register_gpio_i2c_instance(sm, iic);
1194 if (ret < 0)
1195 return ret;
1196 }
1197
1198 return 0;
1199}
1200
Ben Dooksb6d64542007-02-20 13:58:01 -08001201/* sm501_dbg_regs
1202 *
1203 * Debug attribute to attach to parent device to show core registers
1204*/
1205
1206static ssize_t sm501_dbg_regs(struct device *dev,
1207 struct device_attribute *attr, char *buff)
1208{
1209 struct sm501_devdata *sm = dev_get_drvdata(dev) ;
1210 unsigned int reg;
1211 char *ptr = buff;
1212 int ret;
1213
1214 for (reg = 0x00; reg < 0x70; reg += 4) {
1215 ret = sprintf(ptr, "%08x = %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001216 reg, smc501_readl(sm->regs + reg));
Ben Dooksb6d64542007-02-20 13:58:01 -08001217 ptr += ret;
1218 }
1219
1220 return ptr - buff;
1221}
1222
1223
Guenter Roeck8a8320c2013-09-08 00:25:36 -07001224static DEVICE_ATTR(dbg_regs, 0444, sm501_dbg_regs, NULL);
Ben Dooksb6d64542007-02-20 13:58:01 -08001225
1226/* sm501_init_reg
1227 *
1228 * Helper function for the init code to setup a register
Ben Dooks51362372007-06-23 17:16:29 -07001229 *
1230 * clear the bits which are set in r->mask, and then set
1231 * the bits set in r->set.
Ben Dooksb6d64542007-02-20 13:58:01 -08001232*/
1233
1234static inline void sm501_init_reg(struct sm501_devdata *sm,
1235 unsigned long reg,
1236 struct sm501_reg_init *r)
1237{
1238 unsigned long tmp;
1239
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001240 tmp = smc501_readl(sm->regs + reg);
Ben Dooksb6d64542007-02-20 13:58:01 -08001241 tmp &= ~r->mask;
Ben Dooks51362372007-06-23 17:16:29 -07001242 tmp |= r->set;
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001243 smc501_writel(tmp, sm->regs + reg);
Ben Dooksb6d64542007-02-20 13:58:01 -08001244}
1245
1246/* sm501_init_regs
1247 *
1248 * Setup core register values
1249*/
1250
1251static void sm501_init_regs(struct sm501_devdata *sm,
1252 struct sm501_initdata *init)
1253{
1254 sm501_misc_control(sm->dev,
1255 init->misc_control.set,
1256 init->misc_control.mask);
1257
1258 sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
1259 sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
1260 sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
1261
Ben Dooksb6d64542007-02-20 13:58:01 -08001262 if (init->m1xclk) {
1263 dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
1264 sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
1265 }
Ben Dooksb5913bb2007-06-23 17:16:29 -07001266
1267 if (init->mclk) {
1268 dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
1269 sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
1270 }
Ben Dooks81906222007-06-23 17:16:30 -07001271
1272}
1273
1274/* Check the PLL sources for the M1CLK and M1XCLK
1275 *
1276 * If the M1CLK and M1XCLKs are not sourced from the same PLL, then
1277 * there is a risk (see errata AB-5) that the SM501 will cease proper
1278 * function. If this happens, then it is likely the SM501 will
1279 * hang the system.
1280*/
1281
1282static int sm501_check_clocks(struct sm501_devdata *sm)
1283{
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001284 unsigned long pwrmode = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ben Dooks81906222007-06-23 17:16:30 -07001285 unsigned long msrc = (pwrmode & SM501_POWERMODE_M_SRC);
1286 unsigned long m1src = (pwrmode & SM501_POWERMODE_M1_SRC);
1287
1288 return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
Ben Dooksb6d64542007-02-20 13:58:01 -08001289}
1290
1291static unsigned int sm501_mem_local[] = {
1292 [0] = 4*1024*1024,
1293 [1] = 8*1024*1024,
1294 [2] = 16*1024*1024,
1295 [3] = 32*1024*1024,
1296 [4] = 64*1024*1024,
1297 [5] = 2*1024*1024,
1298};
1299
1300/* sm501_init_dev
1301 *
1302 * Common init code for an SM501
1303*/
1304
Bill Pembertonf791be42012-11-19 13:23:04 -05001305static int sm501_init_dev(struct sm501_devdata *sm)
Ben Dooksb6d64542007-02-20 13:58:01 -08001306{
Magnus Damm61711f82008-04-28 02:14:22 -07001307 struct sm501_initdata *idata;
Ben Dooks42cd2362008-07-25 01:46:01 -07001308 struct sm501_platdata *pdata;
Ben Dooksb6d64542007-02-20 13:58:01 -08001309 resource_size_t mem_avail;
1310 unsigned long dramctrl;
Ben Dooks1e27dbe2007-06-23 17:16:31 -07001311 unsigned long devid;
Ben Dooksb6d64542007-02-20 13:58:01 -08001312 int ret;
1313
1314 mutex_init(&sm->clock_lock);
1315 spin_lock_init(&sm->reg_lock);
1316
1317 INIT_LIST_HEAD(&sm->devices);
1318
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001319 devid = smc501_readl(sm->regs + SM501_DEVICEID);
Ben Dooksb6d64542007-02-20 13:58:01 -08001320
Ben Dooks1e27dbe2007-06-23 17:16:31 -07001321 if ((devid & SM501_DEVICEID_IDMASK) != SM501_DEVICEID_SM501) {
1322 dev_err(sm->dev, "incorrect device id %08lx\n", devid);
1323 return -EINVAL;
1324 }
1325
Magnus Damm61711f82008-04-28 02:14:22 -07001326 /* disable irqs */
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001327 smc501_writel(0, sm->regs + SM501_IRQ_MASK);
Magnus Damm61711f82008-04-28 02:14:22 -07001328
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001329 dramctrl = smc501_readl(sm->regs + SM501_DRAM_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -08001330 mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
1331
Ben Dooks1e27dbe2007-06-23 17:16:31 -07001332 dev_info(sm->dev, "SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
1333 sm->regs, devid, (unsigned long)mem_avail >> 20, sm->irq);
Ben Dooksb6d64542007-02-20 13:58:01 -08001334
Ville Syrjala3149be52008-03-04 14:28:50 -08001335 sm->rev = devid & SM501_DEVICEID_REVMASK;
1336
Ben Dooks331d7472007-06-23 17:16:28 -07001337 sm501_dump_gate(sm);
Ben Dooksb6d64542007-02-20 13:58:01 -08001338
1339 ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
1340 if (ret)
1341 dev_err(sm->dev, "failed to create debug regs file\n");
1342
1343 sm501_dump_clk(sm);
1344
1345 /* check to see if we have some device initialisation */
1346
Ben Dooks42cd2362008-07-25 01:46:01 -07001347 pdata = sm->platdata;
1348 idata = pdata ? pdata->init : NULL;
1349
Magnus Damm61711f82008-04-28 02:14:22 -07001350 if (idata) {
1351 sm501_init_regs(sm, idata);
Ben Dooksb6d64542007-02-20 13:58:01 -08001352
Magnus Damm61711f82008-04-28 02:14:22 -07001353 if (idata->devices & SM501_USE_USB_HOST)
1354 sm501_register_usbhost(sm, &mem_avail);
1355 if (idata->devices & (SM501_USE_UART0 | SM501_USE_UART1))
1356 sm501_register_uart(sm, idata->devices);
Ben Dooksf61be272008-07-25 01:45:59 -07001357 if (idata->devices & SM501_USE_GPIO)
1358 sm501_register_gpio(sm);
Ben Dooksb6d64542007-02-20 13:58:01 -08001359 }
1360
Markus Elfring42021512018-03-08 14:20:06 +01001361 if (pdata && pdata->gpio_i2c && pdata->gpio_i2c_nr > 0) {
Ben Dooksf2999202008-07-25 01:46:02 -07001362 if (!sm501_gpio_isregistered(sm))
1363 dev_err(sm->dev, "no gpio available for i2c gpio.\n");
Ben Dooks42cd2362008-07-25 01:46:01 -07001364 else
1365 sm501_register_gpio_i2c(sm, pdata);
1366 }
1367
Ben Dooks81906222007-06-23 17:16:30 -07001368 ret = sm501_check_clocks(sm);
1369 if (ret) {
1370 dev_err(sm->dev, "M1X and M clocks sourced from different "
1371 "PLLs\n");
1372 return -EINVAL;
1373 }
1374
Ben Dooksb6d64542007-02-20 13:58:01 -08001375 /* always create a framebuffer */
1376 sm501_register_display(sm, &mem_avail);
1377
1378 return 0;
1379}
1380
Bill Pembertonf791be42012-11-19 13:23:04 -05001381static int sm501_plat_probe(struct platform_device *dev)
Ben Dooksb6d64542007-02-20 13:58:01 -08001382{
1383 struct sm501_devdata *sm;
Roel Kluin7cf52442008-10-22 01:39:55 +02001384 int ret;
Ben Dooksb6d64542007-02-20 13:58:01 -08001385
Markus Elfring3eec4fa2018-03-08 14:05:41 +01001386 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
Markus Elfring42021512018-03-08 14:20:06 +01001387 if (!sm) {
Roel Kluin7cf52442008-10-22 01:39:55 +02001388 ret = -ENOMEM;
Ben Dooksb6d64542007-02-20 13:58:01 -08001389 goto err1;
1390 }
1391
1392 sm->dev = &dev->dev;
1393 sm->pdev_id = dev->id;
Jingoo Han334a41ce2013-07-30 17:10:05 +09001394 sm->platdata = dev_get_platdata(&dev->dev);
Ben Dooksb6d64542007-02-20 13:58:01 -08001395
Roel Kluin7cf52442008-10-22 01:39:55 +02001396 ret = platform_get_irq(dev, 0);
Stephen Boyd802d9bd2019-07-30 11:15:27 -07001397 if (ret < 0)
Ben Dooksb6d64542007-02-20 13:58:01 -08001398 goto err_res;
Roel Kluin7cf52442008-10-22 01:39:55 +02001399 sm->irq = ret;
Ben Dooksb6d64542007-02-20 13:58:01 -08001400
Roel Kluin7cf52442008-10-22 01:39:55 +02001401 sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
1402 sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Markus Elfring42021512018-03-08 14:20:06 +01001403 if (!sm->io_res || !sm->mem_res) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001404 dev_err(&dev->dev, "failed to get IO resource\n");
Roel Kluin7cf52442008-10-22 01:39:55 +02001405 ret = -ENOENT;
Ben Dooksb6d64542007-02-20 13:58:01 -08001406 goto err_res;
1407 }
1408
1409 sm->regs_claim = request_mem_region(sm->io_res->start,
Samuel Ortiza5300dc2010-03-08 20:07:48 +01001410 0x100, "sm501");
Markus Elfring42021512018-03-08 14:20:06 +01001411 if (!sm->regs_claim) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001412 dev_err(&dev->dev, "cannot claim registers\n");
Roel Kluin7cf52442008-10-22 01:39:55 +02001413 ret = -EBUSY;
Ben Dooksb6d64542007-02-20 13:58:01 -08001414 goto err_res;
1415 }
1416
1417 platform_set_drvdata(dev, sm);
1418
H Hartley Sweeten311e54c2010-01-05 19:55:54 +01001419 sm->regs = ioremap(sm->io_res->start, resource_size(sm->io_res));
Markus Elfring42021512018-03-08 14:20:06 +01001420 if (!sm->regs) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001421 dev_err(&dev->dev, "cannot remap registers\n");
Roel Kluin7cf52442008-10-22 01:39:55 +02001422 ret = -EIO;
Ben Dooksb6d64542007-02-20 13:58:01 -08001423 goto err_claim;
1424 }
1425
1426 return sm501_init_dev(sm);
1427
1428 err_claim:
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001429 release_mem_region(sm->io_res->start, 0x100);
Ben Dooksb6d64542007-02-20 13:58:01 -08001430 err_res:
1431 kfree(sm);
1432 err1:
Roel Kluin7cf52442008-10-22 01:39:55 +02001433 return ret;
Ben Dooksb6d64542007-02-20 13:58:01 -08001434
1435}
1436
Ben Dooks331d7472007-06-23 17:16:28 -07001437#ifdef CONFIG_PM
Ben Dooks472dba72008-07-25 01:45:58 -07001438
Ben Dooks331d7472007-06-23 17:16:28 -07001439/* power management support */
1440
Ben Dooks472dba72008-07-25 01:45:58 -07001441static void sm501_set_power(struct sm501_devdata *sm, int on)
1442{
1443 struct sm501_platdata *pd = sm->platdata;
1444
Markus Elfring42021512018-03-08 14:20:06 +01001445 if (!pd)
Ben Dooks472dba72008-07-25 01:45:58 -07001446 return;
1447
1448 if (pd->get_power) {
1449 if (pd->get_power(sm->dev) == on) {
1450 dev_dbg(sm->dev, "is already %d\n", on);
1451 return;
1452 }
1453 }
1454
1455 if (pd->set_power) {
1456 dev_dbg(sm->dev, "setting power to %d\n", on);
1457
1458 pd->set_power(sm->dev, on);
1459 sm501_mdelay(sm, 10);
1460 }
1461}
1462
Ben Dooks331d7472007-06-23 17:16:28 -07001463static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
1464{
1465 struct sm501_devdata *sm = platform_get_drvdata(pdev);
1466
1467 sm->in_suspend = 1;
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001468 sm->pm_misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
Ben Dooks331d7472007-06-23 17:16:28 -07001469
1470 sm501_dump_regs(sm);
Ben Dooks472dba72008-07-25 01:45:58 -07001471
1472 if (sm->platdata) {
1473 if (sm->platdata->flags & SM501_FLAG_SUSPEND_OFF)
1474 sm501_set_power(sm, 0);
1475 }
1476
Ben Dooks331d7472007-06-23 17:16:28 -07001477 return 0;
1478}
1479
1480static int sm501_plat_resume(struct platform_device *pdev)
1481{
1482 struct sm501_devdata *sm = platform_get_drvdata(pdev);
1483
Ben Dooks472dba72008-07-25 01:45:58 -07001484 sm501_set_power(sm, 1);
1485
Ben Dooks331d7472007-06-23 17:16:28 -07001486 sm501_dump_regs(sm);
1487 sm501_dump_gate(sm);
1488 sm501_dump_clk(sm);
1489
1490 /* check to see if we are in the same state as when suspended */
1491
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001492 if (smc501_readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
Ben Dooks331d7472007-06-23 17:16:28 -07001493 dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001494 smc501_writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
Ben Dooks331d7472007-06-23 17:16:28 -07001495
1496 /* our suspend causes the controller state to change,
1497 * either by something attempting setup, power loss,
1498 * or an external reset event on power change */
1499
1500 if (sm->platdata && sm->platdata->init) {
1501 sm501_init_regs(sm, sm->platdata->init);
1502 }
1503 }
1504
1505 /* dump our state from resume */
1506
1507 sm501_dump_regs(sm);
1508 sm501_dump_clk(sm);
1509
1510 sm->in_suspend = 0;
1511
1512 return 0;
1513}
1514#else
1515#define sm501_plat_suspend NULL
1516#define sm501_plat_resume NULL
1517#endif
1518
Ben Dooksb6d64542007-02-20 13:58:01 -08001519/* Initialisation data for PCI devices */
1520
1521static struct sm501_initdata sm501_pci_initdata = {
1522 .gpio_high = {
1523 .set = 0x3F000000, /* 24bit panel */
1524 .mask = 0x0,
1525 },
1526 .misc_timing = {
1527 .set = 0x010100, /* SDRAM timing */
1528 .mask = 0x1F1F00,
1529 },
1530 .misc_control = {
1531 .set = SM501_MISC_PNL_24BIT,
1532 .mask = 0,
1533 },
1534
1535 .devices = SM501_USE_ALL,
Ben Dooks81906222007-06-23 17:16:30 -07001536
1537 /* Errata AB-3 says that 72MHz is the fastest available
1538 * for 33MHZ PCI with proper bus-mastering operation */
1539
1540 .mclk = 72 * MHZ,
1541 .m1xclk = 144 * MHZ,
Ben Dooksb6d64542007-02-20 13:58:01 -08001542};
1543
1544static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
1545 .flags = (SM501FB_FLAG_USE_INIT_MODE |
1546 SM501FB_FLAG_USE_HWCURSOR |
1547 SM501FB_FLAG_USE_HWACCEL |
1548 SM501FB_FLAG_DISABLE_AT_EXIT),
1549};
1550
1551static struct sm501_platdata_fb sm501_fb_pdata = {
1552 .fb_route = SM501_FB_OWN,
1553 .fb_crt = &sm501_pdata_fbsub,
1554 .fb_pnl = &sm501_pdata_fbsub,
1555};
1556
1557static struct sm501_platdata sm501_pci_platdata = {
1558 .init = &sm501_pci_initdata,
1559 .fb = &sm501_fb_pdata,
Arnaud Patard60e540d2008-07-25 01:46:00 -07001560 .gpio_base = -1,
Ben Dooksb6d64542007-02-20 13:58:01 -08001561};
1562
Bill Pembertonf791be42012-11-19 13:23:04 -05001563static int sm501_pci_probe(struct pci_dev *dev,
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001564 const struct pci_device_id *id)
Ben Dooksb6d64542007-02-20 13:58:01 -08001565{
1566 struct sm501_devdata *sm;
1567 int err;
1568
Markus Elfring3eec4fa2018-03-08 14:05:41 +01001569 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
Markus Elfring42021512018-03-08 14:20:06 +01001570 if (!sm) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001571 err = -ENOMEM;
1572 goto err1;
1573 }
1574
1575 /* set a default set of platform data */
1576 dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
1577
1578 /* set a hopefully unique id for our child platform devices */
1579 sm->pdev_id = 32 + dev->devfn;
1580
1581 pci_set_drvdata(dev, sm);
1582
1583 err = pci_enable_device(dev);
1584 if (err) {
1585 dev_err(&dev->dev, "cannot enable device\n");
1586 goto err2;
1587 }
1588
1589 sm->dev = &dev->dev;
1590 sm->irq = dev->irq;
1591
1592#ifdef __BIG_ENDIAN
1593 /* if the system is big-endian, we most probably have a
1594 * translation in the IO layer making the PCI bus little endian
1595 * so make the framebuffer swapped pixels */
1596
1597 sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
1598#endif
1599
1600 /* check our resources */
1601
1602 if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
1603 dev_err(&dev->dev, "region #0 is not memory?\n");
1604 err = -EINVAL;
1605 goto err3;
1606 }
1607
1608 if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
1609 dev_err(&dev->dev, "region #1 is not memory?\n");
1610 err = -EINVAL;
1611 goto err3;
1612 }
1613
1614 /* make our resources ready for sharing */
1615
1616 sm->io_res = &dev->resource[1];
1617 sm->mem_res = &dev->resource[0];
1618
1619 sm->regs_claim = request_mem_region(sm->io_res->start,
Samuel Ortiza5300dc2010-03-08 20:07:48 +01001620 0x100, "sm501");
Markus Elfring42021512018-03-08 14:20:06 +01001621 if (!sm->regs_claim) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001622 dev_err(&dev->dev, "cannot claim registers\n");
1623 err= -EBUSY;
1624 goto err3;
1625 }
1626
Arjan van de Ven7ab18992008-09-28 16:14:52 -07001627 sm->regs = pci_ioremap_bar(dev, 1);
Markus Elfring42021512018-03-08 14:20:06 +01001628 if (!sm->regs) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001629 dev_err(&dev->dev, "cannot remap registers\n");
1630 err = -EIO;
1631 goto err4;
1632 }
1633
1634 sm501_init_dev(sm);
1635 return 0;
1636
1637 err4:
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001638 release_mem_region(sm->io_res->start, 0x100);
Ben Dooksb6d64542007-02-20 13:58:01 -08001639 err3:
1640 pci_disable_device(dev);
1641 err2:
Ben Dooksb6d64542007-02-20 13:58:01 -08001642 kfree(sm);
1643 err1:
1644 return err;
1645}
1646
1647static void sm501_remove_sub(struct sm501_devdata *sm,
1648 struct sm501_device *smdev)
1649{
1650 list_del(&smdev->list);
1651 platform_device_unregister(&smdev->pdev);
1652}
1653
1654static void sm501_dev_remove(struct sm501_devdata *sm)
1655{
1656 struct sm501_device *smdev, *tmp;
1657
1658 list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
1659 sm501_remove_sub(sm, smdev);
1660
1661 device_remove_file(sm->dev, &dev_attr_dbg_regs);
Ben Dooksf61be272008-07-25 01:45:59 -07001662
Ben Dooksf2999202008-07-25 01:46:02 -07001663 sm501_gpio_remove(sm);
Ben Dooksb6d64542007-02-20 13:58:01 -08001664}
1665
Bill Pemberton4740f732012-11-19 13:26:01 -05001666static void sm501_pci_remove(struct pci_dev *dev)
Ben Dooksb6d64542007-02-20 13:58:01 -08001667{
1668 struct sm501_devdata *sm = pci_get_drvdata(dev);
1669
1670 sm501_dev_remove(sm);
1671 iounmap(sm->regs);
1672
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001673 release_mem_region(sm->io_res->start, 0x100);
Ben Dooksb6d64542007-02-20 13:58:01 -08001674
Ben Dooksb6d64542007-02-20 13:58:01 -08001675 pci_disable_device(dev);
1676}
1677
1678static int sm501_plat_remove(struct platform_device *dev)
1679{
1680 struct sm501_devdata *sm = platform_get_drvdata(dev);
1681
1682 sm501_dev_remove(sm);
1683 iounmap(sm->regs);
1684
Chuhong Yuanb88aa852019-11-16 23:13:08 +08001685 release_mem_region(sm->io_res->start, 0x100);
Ben Dooksb6d64542007-02-20 13:58:01 -08001686
1687 return 0;
1688}
1689
Jingoo Han36fcd062013-12-03 08:15:39 +09001690static const struct pci_device_id sm501_pci_tbl[] = {
Ben Dooksb6d64542007-02-20 13:58:01 -08001691 { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1692 { 0, },
1693};
1694
1695MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
1696
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001697static struct pci_driver sm501_pci_driver = {
Ben Dooksb6d64542007-02-20 13:58:01 -08001698 .name = "sm501",
1699 .id_table = sm501_pci_tbl,
1700 .probe = sm501_pci_probe,
Bill Pemberton84449212012-11-19 13:20:24 -05001701 .remove = sm501_pci_remove,
Ben Dooksb6d64542007-02-20 13:58:01 -08001702};
1703
Kay Sievers4f46d6e2008-07-25 01:45:47 -07001704MODULE_ALIAS("platform:sm501");
1705
Jingoo Hanae6eee3c2014-05-07 17:24:05 +09001706static const struct of_device_id of_sm501_match_tbl[] = {
Heiko Schocher4295f9b2011-01-26 07:21:30 +00001707 { .compatible = "smi,sm501", },
1708 { /* end */ }
1709};
Luis de Bethencourt327cc182015-09-17 20:16:50 +02001710MODULE_DEVICE_TABLE(of, of_sm501_match_tbl);
Heiko Schocher4295f9b2011-01-26 07:21:30 +00001711
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001712static struct platform_driver sm501_plat_driver = {
Ben Dooksb6d64542007-02-20 13:58:01 -08001713 .driver = {
1714 .name = "sm501",
Heiko Schocher4295f9b2011-01-26 07:21:30 +00001715 .of_match_table = of_sm501_match_tbl,
Ben Dooksb6d64542007-02-20 13:58:01 -08001716 },
1717 .probe = sm501_plat_probe,
1718 .remove = sm501_plat_remove,
Ben Dooks331d7472007-06-23 17:16:28 -07001719 .suspend = sm501_plat_suspend,
1720 .resume = sm501_plat_resume,
Ben Dooksb6d64542007-02-20 13:58:01 -08001721};
1722
1723static int __init sm501_base_init(void)
1724{
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001725 platform_driver_register(&sm501_plat_driver);
1726 return pci_register_driver(&sm501_pci_driver);
Ben Dooksb6d64542007-02-20 13:58:01 -08001727}
1728
1729static void __exit sm501_base_exit(void)
1730{
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001731 platform_driver_unregister(&sm501_plat_driver);
1732 pci_unregister_driver(&sm501_pci_driver);
Ben Dooksb6d64542007-02-20 13:58:01 -08001733}
1734
1735module_init(sm501_base_init);
1736module_exit(sm501_base_exit);
1737
1738MODULE_DESCRIPTION("SM501 Core Driver");
1739MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
1740MODULE_LICENSE("GPL v2");