blob: d5e34a5eb25023751fb841718005f4f47ade3667 [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 Walleijb2e63552017-09-10 01:30:46 +020020#include <linux/gpio/machine.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ben Dooksb6d64542007-02-20 13:58:01 -080022
23#include <linux/sm501.h>
24#include <linux/sm501-regs.h>
Magnus Damm61711f82008-04-28 02:14:22 -070025#include <linux/serial_8250.h>
Ben Dooksb6d64542007-02-20 13:58:01 -080026
Axel Linf77401d2010-11-10 15:47:51 +080027#include <linux/io.h>
Ben Dooksb6d64542007-02-20 13:58:01 -080028
29struct sm501_device {
30 struct list_head list;
31 struct platform_device pdev;
32};
33
Ben Dooksf61be272008-07-25 01:45:59 -070034struct sm501_gpio;
35
Ben Dooksf2999202008-07-25 01:46:02 -070036#ifdef CONFIG_MFD_SM501_GPIO
37#include <linux/gpio.h>
38
Ben Dooksf61be272008-07-25 01:45:59 -070039struct sm501_gpio_chip {
40 struct gpio_chip gpio;
41 struct sm501_gpio *ourgpio; /* to get back to parent. */
42 void __iomem *regbase;
Ben Dooks98325f82009-01-10 16:59:53 +010043 void __iomem *control; /* address of control reg. */
Ben Dooksf61be272008-07-25 01:45:59 -070044};
45
46struct sm501_gpio {
47 struct sm501_gpio_chip low;
48 struct sm501_gpio_chip high;
49 spinlock_t lock;
50
51 unsigned int registered : 1;
52 void __iomem *regs;
53 struct resource *regs_res;
54};
Ben Dooksf2999202008-07-25 01:46:02 -070055#else
56struct sm501_gpio {
57 /* no gpio support, empty definition for sm501_devdata. */
58};
59#endif
Ben Dooksf61be272008-07-25 01:45:59 -070060
Ben Dooksb6d64542007-02-20 13:58:01 -080061struct sm501_devdata {
62 spinlock_t reg_lock;
63 struct mutex clock_lock;
64 struct list_head devices;
Ben Dooksf61be272008-07-25 01:45:59 -070065 struct sm501_gpio gpio;
Ben Dooksb6d64542007-02-20 13:58:01 -080066
67 struct device *dev;
68 struct resource *io_res;
69 struct resource *mem_res;
70 struct resource *regs_claim;
71 struct sm501_platdata *platdata;
72
Ben Dooksf61be272008-07-25 01:45:59 -070073
Ben Dooks331d7472007-06-23 17:16:28 -070074 unsigned int in_suspend;
75 unsigned long pm_misc;
76
Ben Dooksb6d64542007-02-20 13:58:01 -080077 int unit_power[20];
78 unsigned int pdev_id;
79 unsigned int irq;
80 void __iomem *regs;
Ville Syrjala3149be52008-03-04 14:28:50 -080081 unsigned int rev;
Ben Dooksb6d64542007-02-20 13:58:01 -080082};
83
Ben Dooksf61be272008-07-25 01:45:59 -070084
Ben Dooksb6d64542007-02-20 13:58:01 -080085#define MHZ (1000 * 1000)
86
87#ifdef DEBUG
Ville Syrjala245904a2008-03-04 14:28:49 -080088static const unsigned int div_tab[] = {
Ben Dooksb6d64542007-02-20 13:58:01 -080089 [0] = 1,
90 [1] = 2,
91 [2] = 4,
92 [3] = 8,
93 [4] = 16,
94 [5] = 32,
95 [6] = 64,
96 [7] = 128,
97 [8] = 3,
98 [9] = 6,
99 [10] = 12,
100 [11] = 24,
101 [12] = 48,
102 [13] = 96,
103 [14] = 192,
104 [15] = 384,
105 [16] = 5,
106 [17] = 10,
107 [18] = 20,
108 [19] = 40,
109 [20] = 80,
110 [21] = 160,
111 [22] = 320,
112 [23] = 604,
113};
114
115static unsigned long decode_div(unsigned long pll2, unsigned long val,
116 unsigned int lshft, unsigned int selbit,
Ville Syrjala245904a2008-03-04 14:28:49 -0800117 unsigned long mask)
Ben Dooksb6d64542007-02-20 13:58:01 -0800118{
119 if (val & selbit)
120 pll2 = 288 * MHZ;
121
Ville Syrjala245904a2008-03-04 14:28:49 -0800122 return pll2 / div_tab[(val >> lshft) & mask];
Ben Dooksb6d64542007-02-20 13:58:01 -0800123}
124
125#define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
126
127/* sm501_dump_clk
128 *
129 * Print out the current clock configuration for the device
130*/
131
132static void sm501_dump_clk(struct sm501_devdata *sm)
133{
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000134 unsigned long misct = smc501_readl(sm->regs + SM501_MISC_TIMING);
135 unsigned long pm0 = smc501_readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
136 unsigned long pm1 = smc501_readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
137 unsigned long pmc = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800138 unsigned long sdclk0, sdclk1;
139 unsigned long pll2 = 0;
140
141 switch (misct & 0x30) {
142 case 0x00:
143 pll2 = 336 * MHZ;
144 break;
145 case 0x10:
146 pll2 = 288 * MHZ;
147 break;
148 case 0x20:
149 pll2 = 240 * MHZ;
150 break;
151 case 0x30:
152 pll2 = 192 * MHZ;
153 break;
154 }
155
156 sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
Ville Syrjala245904a2008-03-04 14:28:49 -0800157 sdclk0 /= div_tab[((misct >> 8) & 0xf)];
Ben Dooksb6d64542007-02-20 13:58:01 -0800158
159 sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
Ville Syrjala245904a2008-03-04 14:28:49 -0800160 sdclk1 /= div_tab[((misct >> 16) & 0xf)];
Ben Dooksb6d64542007-02-20 13:58:01 -0800161
162 dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
163 misct, pm0, pm1);
164
165 dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
166 fmt_freq(pll2), sdclk0, sdclk1);
167
168 dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
169
170 dev_dbg(sm->dev, "PM0[%c]: "
171 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
Ben Dooks48986f02007-11-28 16:21:37 -0800172 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
Ben Dooksb6d64542007-02-20 13:58:01 -0800173 (pmc & 3 ) == 0 ? '*' : '-',
Ville Syrjala245904a2008-03-04 14:28:49 -0800174 fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
175 fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
176 fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15)),
177 fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15)));
Ben Dooksb6d64542007-02-20 13:58:01 -0800178
179 dev_dbg(sm->dev, "PM1[%c]: "
180 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
181 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
182 (pmc & 3 ) == 1 ? '*' : '-',
Ville Syrjala245904a2008-03-04 14:28:49 -0800183 fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
184 fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
185 fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15)),
186 fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15)));
Ben Dooksb6d64542007-02-20 13:58:01 -0800187}
Ben Dooks331d7472007-06-23 17:16:28 -0700188
189static void sm501_dump_regs(struct sm501_devdata *sm)
Ben Dooksb6d64542007-02-20 13:58:01 -0800190{
Ben Dooks331d7472007-06-23 17:16:28 -0700191 void __iomem *regs = sm->regs;
192
193 dev_info(sm->dev, "System Control %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000194 smc501_readl(regs + SM501_SYSTEM_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700195 dev_info(sm->dev, "Misc Control %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000196 smc501_readl(regs + SM501_MISC_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700197 dev_info(sm->dev, "GPIO Control Low %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000198 smc501_readl(regs + SM501_GPIO31_0_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700199 dev_info(sm->dev, "GPIO Control Hi %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000200 smc501_readl(regs + SM501_GPIO63_32_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700201 dev_info(sm->dev, "DRAM Control %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000202 smc501_readl(regs + SM501_DRAM_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700203 dev_info(sm->dev, "Arbitration Ctrl %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000204 smc501_readl(regs + SM501_ARBTRTN_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700205 dev_info(sm->dev, "Misc Timing %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000206 smc501_readl(regs + SM501_MISC_TIMING));
Ben Dooksb6d64542007-02-20 13:58:01 -0800207}
Ben Dooks331d7472007-06-23 17:16:28 -0700208
209static void sm501_dump_gate(struct sm501_devdata *sm)
210{
211 dev_info(sm->dev, "CurrentGate %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000212 smc501_readl(sm->regs + SM501_CURRENT_GATE));
Ben Dooks331d7472007-06-23 17:16:28 -0700213 dev_info(sm->dev, "CurrentClock %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000214 smc501_readl(sm->regs + SM501_CURRENT_CLOCK));
Ben Dooks331d7472007-06-23 17:16:28 -0700215 dev_info(sm->dev, "PowerModeControl %08x\n",
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000216 smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL));
Ben Dooks331d7472007-06-23 17:16:28 -0700217}
218
219#else
220static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
221static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
222static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
Ben Dooksb6d64542007-02-20 13:58:01 -0800223#endif
224
225/* sm501_sync_regs
226 *
227 * ensure the
228*/
229
230static void sm501_sync_regs(struct sm501_devdata *sm)
231{
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000232 smc501_readl(sm->regs);
Ben Dooksb6d64542007-02-20 13:58:01 -0800233}
234
Ben Dooks331d7472007-06-23 17:16:28 -0700235static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
236{
237 /* during suspend/resume, we are currently not allowed to sleep,
238 * so change to using mdelay() instead of msleep() if we
239 * are in one of these paths */
240
241 if (sm->in_suspend)
242 mdelay(delay);
243 else
244 msleep(delay);
245}
246
Ben Dooksb6d64542007-02-20 13:58:01 -0800247/* sm501_misc_control
248 *
Ben Dooks331d7472007-06-23 17:16:28 -0700249 * alters the miscellaneous control parameters
Ben Dooksb6d64542007-02-20 13:58:01 -0800250*/
251
252int sm501_misc_control(struct device *dev,
253 unsigned long set, unsigned long clear)
254{
255 struct sm501_devdata *sm = dev_get_drvdata(dev);
256 unsigned long misc;
257 unsigned long save;
258 unsigned long to;
259
260 spin_lock_irqsave(&sm->reg_lock, save);
261
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000262 misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800263 to = (misc & ~clear) | set;
264
265 if (to != misc) {
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000266 smc501_writel(to, sm->regs + SM501_MISC_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800267 sm501_sync_regs(sm);
268
269 dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
270 }
271
272 spin_unlock_irqrestore(&sm->reg_lock, save);
273 return to;
274}
275
276EXPORT_SYMBOL_GPL(sm501_misc_control);
277
278/* sm501_modify_reg
279 *
280 * Modify a register in the SM501 which may be shared with other
281 * drivers.
282*/
283
284unsigned long sm501_modify_reg(struct device *dev,
285 unsigned long reg,
286 unsigned long set,
287 unsigned long clear)
288{
289 struct sm501_devdata *sm = dev_get_drvdata(dev);
290 unsigned long data;
291 unsigned long save;
292
293 spin_lock_irqsave(&sm->reg_lock, save);
294
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000295 data = smc501_readl(sm->regs + reg);
Ben Dooksb6d64542007-02-20 13:58:01 -0800296 data |= set;
297 data &= ~clear;
298
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000299 smc501_writel(data, sm->regs + reg);
Ben Dooksb6d64542007-02-20 13:58:01 -0800300 sm501_sync_regs(sm);
301
302 spin_unlock_irqrestore(&sm->reg_lock, save);
303
304 return data;
305}
306
307EXPORT_SYMBOL_GPL(sm501_modify_reg);
308
Ben Dooksb6d64542007-02-20 13:58:01 -0800309/* sm501_unit_power
310 *
311 * alters the power active gate to set specific units on or off
312 */
313
314int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
315{
316 struct sm501_devdata *sm = dev_get_drvdata(dev);
317 unsigned long mode;
318 unsigned long gate;
319 unsigned long clock;
320
321 mutex_lock(&sm->clock_lock);
322
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000323 mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
324 gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
325 clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800326
327 mode &= 3; /* get current power mode */
328
Adrian Bunkbf703c32007-04-01 23:49:38 -0700329 if (unit >= ARRAY_SIZE(sm->unit_power)) {
Harvey Harrison145980a2008-04-30 00:54:57 -0700330 dev_err(dev, "%s: bad unit %d\n", __func__, unit);
Ben Dooksb6d64542007-02-20 13:58:01 -0800331 goto already;
332 }
333
Harvey Harrison145980a2008-04-30 00:54:57 -0700334 dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __func__, unit,
Ben Dooksb6d64542007-02-20 13:58:01 -0800335 sm->unit_power[unit], to);
336
337 if (to == 0 && sm->unit_power[unit] == 0) {
338 dev_err(sm->dev, "unit %d is already shutdown\n", unit);
339 goto already;
340 }
341
342 sm->unit_power[unit] += to ? 1 : -1;
343 to = sm->unit_power[unit] ? 1 : 0;
344
345 if (to) {
346 if (gate & (1 << unit))
347 goto already;
348 gate |= (1 << unit);
349 } else {
350 if (!(gate & (1 << unit)))
351 goto already;
352 gate &= ~(1 << unit);
353 }
354
355 switch (mode) {
356 case 1:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000357 smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
358 smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800359 mode = 0;
360 break;
361 case 2:
362 case 0:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000363 smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
364 smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800365 mode = 1;
366 break;
367
368 default:
Jiri Slaby992bb252009-06-22 00:00:02 +0200369 gate = -1;
370 goto already;
Ben Dooksb6d64542007-02-20 13:58:01 -0800371 }
372
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000373 smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800374 sm501_sync_regs(sm);
375
376 dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
377 gate, clock, mode);
378
Ben Dooks331d7472007-06-23 17:16:28 -0700379 sm501_mdelay(sm, 16);
Ben Dooksb6d64542007-02-20 13:58:01 -0800380
381 already:
382 mutex_unlock(&sm->clock_lock);
383 return gate;
384}
385
386EXPORT_SYMBOL_GPL(sm501_unit_power);
387
Ben Dooksb6d64542007-02-20 13:58:01 -0800388/* clock value structure. */
389struct sm501_clock {
390 unsigned long mclk;
391 int divider;
392 int shift;
Ville Syrjala3149be52008-03-04 14:28:50 -0800393 unsigned int m, n, k;
Ben Dooksb6d64542007-02-20 13:58:01 -0800394};
395
Ville Syrjala3149be52008-03-04 14:28:50 -0800396/* sm501_calc_clock
397 *
398 * Calculates the nearest discrete clock frequency that
399 * can be achieved with the specified input clock.
400 * the maximum divisor is 3 or 5
401 */
402
403static int sm501_calc_clock(unsigned long freq,
404 struct sm501_clock *clock,
405 int max_div,
406 unsigned long mclk,
407 long *best_diff)
408{
409 int ret = 0;
410 int divider;
411 int shift;
412 long diff;
413
414 /* try dividers 1 and 3 for CRT and for panel,
415 try divider 5 for panel only.*/
416
417 for (divider = 1; divider <= max_div; divider += 2) {
418 /* try all 8 shift values.*/
419 for (shift = 0; shift < 8; shift++) {
420 /* Calculate difference to requested clock */
Axel Lin829ecbc2012-03-12 10:12:34 +0800421 diff = DIV_ROUND_CLOSEST(mclk, divider << shift) - freq;
Ville Syrjala3149be52008-03-04 14:28:50 -0800422 if (diff < 0)
423 diff = -diff;
424
425 /* If it is less than the current, use it */
426 if (diff < *best_diff) {
427 *best_diff = diff;
428
429 clock->mclk = mclk;
430 clock->divider = divider;
431 clock->shift = shift;
432 ret = 1;
433 }
434 }
435 }
436
437 return ret;
438}
439
440/* sm501_calc_pll
441 *
442 * Calculates the nearest discrete clock frequency that can be
443 * achieved using the programmable PLL.
444 * the maximum divisor is 3 or 5
445 */
446
447static unsigned long sm501_calc_pll(unsigned long freq,
448 struct sm501_clock *clock,
449 int max_div)
450{
451 unsigned long mclk;
452 unsigned int m, n, k;
453 long best_diff = 999999999;
454
455 /*
456 * The SM502 datasheet doesn't specify the min/max values for M and N.
457 * N = 1 at least doesn't work in practice.
458 */
459 for (m = 2; m <= 255; m++) {
460 for (n = 2; n <= 127; n++) {
461 for (k = 0; k <= 1; k++) {
462 mclk = (24000000UL * m / n) >> k;
463
464 if (sm501_calc_clock(freq, clock, max_div,
465 mclk, &best_diff)) {
466 clock->m = m;
467 clock->n = n;
468 clock->k = k;
469 }
470 }
471 }
472 }
473
474 /* Return best clock. */
475 return clock->mclk / (clock->divider << clock->shift);
476}
477
Ben Dooksb6d64542007-02-20 13:58:01 -0800478/* sm501_select_clock
479 *
Ville Syrjala3149be52008-03-04 14:28:50 -0800480 * Calculates the nearest discrete clock frequency that can be
481 * achieved using the 288MHz and 336MHz PLLs.
Ben Dooksb6d64542007-02-20 13:58:01 -0800482 * the maximum divisor is 3 or 5
483 */
Ville Syrjala3149be52008-03-04 14:28:50 -0800484
Ben Dooksb6d64542007-02-20 13:58:01 -0800485static unsigned long sm501_select_clock(unsigned long freq,
486 struct sm501_clock *clock,
487 int max_div)
488{
489 unsigned long mclk;
Ben Dooksb6d64542007-02-20 13:58:01 -0800490 long best_diff = 999999999;
491
492 /* Try 288MHz and 336MHz clocks. */
493 for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
Ville Syrjala3149be52008-03-04 14:28:50 -0800494 sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
Ben Dooksb6d64542007-02-20 13:58:01 -0800495 }
496
497 /* Return best clock. */
498 return clock->mclk / (clock->divider << clock->shift);
499}
500
501/* sm501_set_clock
502 *
503 * set one of the four clock sources to the closest available frequency to
504 * the one specified
505*/
506
507unsigned long sm501_set_clock(struct device *dev,
508 int clksrc,
509 unsigned long req_freq)
510{
511 struct sm501_devdata *sm = dev_get_drvdata(dev);
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000512 unsigned long mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
513 unsigned long gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
514 unsigned long clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ville Syrjala3149be52008-03-04 14:28:50 -0800515 unsigned int pll_reg = 0;
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800516 unsigned long sm501_freq; /* the actual frequency achieved */
Lee Jones5f114eb2014-08-18 16:11:27 +0100517 u64 reg;
Ben Dooksb6d64542007-02-20 13:58:01 -0800518
519 struct sm501_clock to;
520
521 /* find achivable discrete frequency and setup register value
522 * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
523 * has an extra bit for the divider */
524
525 switch (clksrc) {
526 case SM501_CLOCK_P2XCLK:
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800527 /* This clock is divided in half so to achieve the
Ben Dooksb6d64542007-02-20 13:58:01 -0800528 * requested frequency the value must be multiplied by
529 * 2. This clock also has an additional pre divisor */
530
Ville Syrjala3149be52008-03-04 14:28:50 -0800531 if (sm->rev >= 0xC0) {
532 /* SM502 -> use the programmable PLL */
533 sm501_freq = (sm501_calc_pll(2 * req_freq,
534 &to, 5) / 2);
535 reg = to.shift & 0x07;/* bottom 3 bits are shift */
536 if (to.divider == 3)
537 reg |= 0x08; /* /3 divider required */
538 else if (to.divider == 5)
539 reg |= 0x10; /* /5 divider required */
540 reg |= 0x40; /* select the programmable PLL */
541 pll_reg = 0x20000 | (to.k << 15) | (to.n << 8) | to.m;
542 } else {
543 sm501_freq = (sm501_select_clock(2 * req_freq,
544 &to, 5) / 2);
545 reg = to.shift & 0x07;/* bottom 3 bits are shift */
546 if (to.divider == 3)
547 reg |= 0x08; /* /3 divider required */
548 else if (to.divider == 5)
549 reg |= 0x10; /* /5 divider required */
550 if (to.mclk != 288000000)
551 reg |= 0x20; /* which mclk pll is source */
552 }
Ben Dooksb6d64542007-02-20 13:58:01 -0800553 break;
554
555 case SM501_CLOCK_V2XCLK:
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800556 /* This clock is divided in half so to achieve the
Ben Dooksb6d64542007-02-20 13:58:01 -0800557 * requested frequency the value must be multiplied by 2. */
558
559 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
560 reg=to.shift & 0x07; /* bottom 3 bits are shift */
561 if (to.divider == 3)
562 reg |= 0x08; /* /3 divider required */
563 if (to.mclk != 288000000)
564 reg |= 0x10; /* which mclk pll is source */
565 break;
566
567 case SM501_CLOCK_MCLK:
568 case SM501_CLOCK_M1XCLK:
569 /* These clocks are the same and not further divided */
570
571 sm501_freq = sm501_select_clock( req_freq, &to, 3);
572 reg=to.shift & 0x07; /* bottom 3 bits are shift */
573 if (to.divider == 3)
574 reg |= 0x08; /* /3 divider required */
575 if (to.mclk != 288000000)
576 reg |= 0x10; /* which mclk pll is source */
577 break;
578
579 default:
580 return 0; /* this is bad */
581 }
582
583 mutex_lock(&sm->clock_lock);
584
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000585 mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
586 gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
587 clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800588
589 clock = clock & ~(0xFF << clksrc);
590 clock |= reg<<clksrc;
591
592 mode &= 3; /* find current mode */
593
594 switch (mode) {
595 case 1:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000596 smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
597 smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800598 mode = 0;
599 break;
600 case 2:
601 case 0:
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000602 smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
603 smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
Ben Dooksb6d64542007-02-20 13:58:01 -0800604 mode = 1;
605 break;
606
607 default:
608 mutex_unlock(&sm->clock_lock);
609 return -1;
610 }
611
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000612 smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
Ville Syrjala3149be52008-03-04 14:28:50 -0800613
614 if (pll_reg)
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000615 smc501_writel(pll_reg,
616 sm->regs + SM501_PROGRAMMABLE_PLL_CONTROL);
Ville Syrjala3149be52008-03-04 14:28:50 -0800617
Ben Dooksb6d64542007-02-20 13:58:01 -0800618 sm501_sync_regs(sm);
619
Ben Dooks80e74a82008-08-10 23:45:24 +0200620 dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
621 gate, clock, mode);
Ben Dooksb6d64542007-02-20 13:58:01 -0800622
Ben Dooks331d7472007-06-23 17:16:28 -0700623 sm501_mdelay(sm, 16);
Ben Dooksb6d64542007-02-20 13:58:01 -0800624 mutex_unlock(&sm->clock_lock);
625
626 sm501_dump_clk(sm);
627
628 return sm501_freq;
629}
630
631EXPORT_SYMBOL_GPL(sm501_set_clock);
632
633/* sm501_find_clock
634 *
635 * finds the closest available frequency for a given clock
636*/
637
Ville Syrjala3149be52008-03-04 14:28:50 -0800638unsigned long sm501_find_clock(struct device *dev,
639 int clksrc,
Ben Dooksb6d64542007-02-20 13:58:01 -0800640 unsigned long req_freq)
641{
Ville Syrjala3149be52008-03-04 14:28:50 -0800642 struct sm501_devdata *sm = dev_get_drvdata(dev);
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800643 unsigned long sm501_freq; /* the frequency achieveable by the 501 */
Ben Dooksb6d64542007-02-20 13:58:01 -0800644 struct sm501_clock to;
645
646 switch (clksrc) {
647 case SM501_CLOCK_P2XCLK:
Ville Syrjala3149be52008-03-04 14:28:50 -0800648 if (sm->rev >= 0xC0) {
649 /* SM502 -> use the programmable PLL */
650 sm501_freq = (sm501_calc_pll(2 * req_freq,
651 &to, 5) / 2);
652 } else {
653 sm501_freq = (sm501_select_clock(2 * req_freq,
654 &to, 5) / 2);
655 }
Ben Dooksb6d64542007-02-20 13:58:01 -0800656 break;
657
658 case SM501_CLOCK_V2XCLK:
659 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
660 break;
661
662 case SM501_CLOCK_MCLK:
663 case SM501_CLOCK_M1XCLK:
664 sm501_freq = sm501_select_clock(req_freq, &to, 3);
665 break;
666
667 default:
668 sm501_freq = 0; /* error */
669 }
670
671 return sm501_freq;
672}
673
674EXPORT_SYMBOL_GPL(sm501_find_clock);
675
676static struct sm501_device *to_sm_device(struct platform_device *pdev)
677{
678 return container_of(pdev, struct sm501_device, pdev);
679}
680
681/* sm501_device_release
682 *
683 * A release function for the platform devices we create to allow us to
684 * free any items we allocated
685*/
686
687static void sm501_device_release(struct device *dev)
688{
689 kfree(to_sm_device(to_platform_device(dev)));
690}
691
692/* sm501_create_subdev
693 *
694 * Create a skeleton platform device with resources for passing to a
695 * sub-driver
696*/
697
698static struct platform_device *
Magnus Damm61711f82008-04-28 02:14:22 -0700699sm501_create_subdev(struct sm501_devdata *sm, char *name,
700 unsigned int res_count, unsigned int platform_data_size)
Ben Dooksb6d64542007-02-20 13:58:01 -0800701{
702 struct sm501_device *smdev;
703
704 smdev = kzalloc(sizeof(struct sm501_device) +
Magnus Damm61711f82008-04-28 02:14:22 -0700705 (sizeof(struct resource) * res_count) +
706 platform_data_size, GFP_KERNEL);
Ben Dooksb6d64542007-02-20 13:58:01 -0800707 if (!smdev)
708 return NULL;
709
710 smdev->pdev.dev.release = sm501_device_release;
711
712 smdev->pdev.name = name;
713 smdev->pdev.id = sm->pdev_id;
Ben Dooksb6d64542007-02-20 13:58:01 -0800714 smdev->pdev.dev.parent = sm->dev;
Guenter Roeck2f606da2018-08-03 20:59:51 -0700715 smdev->pdev.dev.coherent_dma_mask = 0xffffffff;
Ben Dooksb6d64542007-02-20 13:58:01 -0800716
Magnus Damm61711f82008-04-28 02:14:22 -0700717 if (res_count) {
718 smdev->pdev.resource = (struct resource *)(smdev+1);
719 smdev->pdev.num_resources = res_count;
720 }
721 if (platform_data_size)
722 smdev->pdev.dev.platform_data = (void *)(smdev+1);
723
Ben Dooksb6d64542007-02-20 13:58:01 -0800724 return &smdev->pdev;
725}
726
727/* sm501_register_device
728 *
729 * Register a platform device created with sm501_create_subdev()
730*/
731
732static int sm501_register_device(struct sm501_devdata *sm,
733 struct platform_device *pdev)
734{
735 struct sm501_device *smdev = to_sm_device(pdev);
736 int ptr;
737 int ret;
738
739 for (ptr = 0; ptr < pdev->num_resources; ptr++) {
Joe Perches3f3d4310b2010-11-12 13:37:56 -0800740 printk(KERN_DEBUG "%s[%d] %pR\n",
741 pdev->name, ptr, &pdev->resource[ptr]);
Ben Dooksb6d64542007-02-20 13:58:01 -0800742 }
743
744 ret = platform_device_register(pdev);
745
746 if (ret >= 0) {
747 dev_dbg(sm->dev, "registered %s\n", pdev->name);
748 list_add_tail(&smdev->list, &sm->devices);
749 } else
750 dev_err(sm->dev, "error registering %s (%d)\n",
751 pdev->name, ret);
752
753 return ret;
754}
755
756/* sm501_create_subio
757 *
758 * Fill in an IO resource for a sub device
759*/
760
761static void sm501_create_subio(struct sm501_devdata *sm,
762 struct resource *res,
763 resource_size_t offs,
764 resource_size_t size)
765{
766 res->flags = IORESOURCE_MEM;
767 res->parent = sm->io_res;
768 res->start = sm->io_res->start + offs;
769 res->end = res->start + size - 1;
770}
771
772/* sm501_create_mem
773 *
774 * Fill in an MEM resource for a sub device
775*/
776
777static void sm501_create_mem(struct sm501_devdata *sm,
778 struct resource *res,
779 resource_size_t *offs,
780 resource_size_t size)
781{
782 *offs -= size; /* adjust memory size */
783
784 res->flags = IORESOURCE_MEM;
785 res->parent = sm->mem_res;
786 res->start = sm->mem_res->start + *offs;
787 res->end = res->start + size - 1;
788}
789
790/* sm501_create_irq
791 *
792 * Fill in an IRQ resource for a sub device
793*/
794
795static void sm501_create_irq(struct sm501_devdata *sm,
796 struct resource *res)
797{
798 res->flags = IORESOURCE_IRQ;
799 res->parent = NULL;
800 res->start = res->end = sm->irq;
801}
802
803static int sm501_register_usbhost(struct sm501_devdata *sm,
804 resource_size_t *mem_avail)
805{
806 struct platform_device *pdev;
807
Magnus Damm61711f82008-04-28 02:14:22 -0700808 pdev = sm501_create_subdev(sm, "sm501-usb", 3, 0);
Ben Dooksb6d64542007-02-20 13:58:01 -0800809 if (!pdev)
810 return -ENOMEM;
811
812 sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
813 sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
814 sm501_create_irq(sm, &pdev->resource[2]);
815
816 return sm501_register_device(sm, pdev);
817}
818
Magnus Damm61711f82008-04-28 02:14:22 -0700819static void sm501_setup_uart_data(struct sm501_devdata *sm,
820 struct plat_serial8250_port *uart_data,
821 unsigned int offset)
822{
823 uart_data->membase = sm->regs + offset;
824 uart_data->mapbase = sm->io_res->start + offset;
825 uart_data->iotype = UPIO_MEM;
826 uart_data->irq = sm->irq;
827 uart_data->flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
828 uart_data->regshift = 2;
829 uart_data->uartclk = (9600 * 16);
830}
831
832static int sm501_register_uart(struct sm501_devdata *sm, int devices)
833{
834 struct platform_device *pdev;
835 struct plat_serial8250_port *uart_data;
836
837 pdev = sm501_create_subdev(sm, "serial8250", 0,
838 sizeof(struct plat_serial8250_port) * 3);
839 if (!pdev)
840 return -ENOMEM;
841
Jingoo Han334a41ce2013-07-30 17:10:05 +0900842 uart_data = dev_get_platdata(&pdev->dev);
Magnus Damm61711f82008-04-28 02:14:22 -0700843
844 if (devices & SM501_USE_UART0) {
845 sm501_setup_uart_data(sm, uart_data++, 0x30000);
846 sm501_unit_power(sm->dev, SM501_GATE_UART0, 1);
847 sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 12, 0);
848 sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x01e0, 0);
849 }
850 if (devices & SM501_USE_UART1) {
851 sm501_setup_uart_data(sm, uart_data++, 0x30020);
852 sm501_unit_power(sm->dev, SM501_GATE_UART1, 1);
853 sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 13, 0);
854 sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x1e00, 0);
855 }
856
857 pdev->id = PLAT8250_DEV_SM501;
858
859 return sm501_register_device(sm, pdev);
860}
861
Ben Dooksb6d64542007-02-20 13:58:01 -0800862static int sm501_register_display(struct sm501_devdata *sm,
863 resource_size_t *mem_avail)
864{
865 struct platform_device *pdev;
866
Magnus Damm61711f82008-04-28 02:14:22 -0700867 pdev = sm501_create_subdev(sm, "sm501-fb", 4, 0);
Ben Dooksb6d64542007-02-20 13:58:01 -0800868 if (!pdev)
869 return -ENOMEM;
870
871 sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
872 sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
873 sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
874 sm501_create_irq(sm, &pdev->resource[3]);
875
876 return sm501_register_device(sm, pdev);
877}
878
Ben Dooksf61be272008-07-25 01:45:59 -0700879#ifdef CONFIG_MFD_SM501_GPIO
880
Ben Dooksf61be272008-07-25 01:45:59 -0700881static inline struct sm501_devdata *sm501_gpio_to_dev(struct sm501_gpio *gpio)
882{
883 return container_of(gpio, struct sm501_devdata, gpio);
884}
885
886static int sm501_gpio_get(struct gpio_chip *chip, unsigned offset)
887
888{
Linus Walleij3a504102016-03-30 10:48:05 +0200889 struct sm501_gpio_chip *smgpio = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700890 unsigned long result;
891
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000892 result = smc501_readl(smgpio->regbase + SM501_GPIO_DATA_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700893 result >>= offset;
894
895 return result & 1UL;
896}
897
Ben Dooks98325f82009-01-10 16:59:53 +0100898static void sm501_gpio_ensure_gpio(struct sm501_gpio_chip *smchip,
899 unsigned long bit)
900{
901 unsigned long ctrl;
902
903 /* check and modify if this pin is not set as gpio. */
904
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000905 if (smc501_readl(smchip->control) & bit) {
Ben Dooks98325f82009-01-10 16:59:53 +0100906 dev_info(sm501_gpio_to_dev(smchip->ourgpio)->dev,
907 "changing mode of gpio, bit %08lx\n", bit);
908
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000909 ctrl = smc501_readl(smchip->control);
Ben Dooks98325f82009-01-10 16:59:53 +0100910 ctrl &= ~bit;
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000911 smc501_writel(ctrl, smchip->control);
Ben Dooks98325f82009-01-10 16:59:53 +0100912
913 sm501_sync_regs(sm501_gpio_to_dev(smchip->ourgpio));
914 }
915}
916
Ben Dooksf61be272008-07-25 01:45:59 -0700917static void sm501_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
918
919{
Linus Walleij3a504102016-03-30 10:48:05 +0200920 struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700921 struct sm501_gpio *smgpio = smchip->ourgpio;
922 unsigned long bit = 1 << offset;
923 void __iomem *regs = smchip->regbase;
924 unsigned long save;
925 unsigned long val;
926
927 dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
928 __func__, chip, offset);
929
930 spin_lock_irqsave(&smgpio->lock, save);
931
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000932 val = smc501_readl(regs + SM501_GPIO_DATA_LOW) & ~bit;
Ben Dooksf61be272008-07-25 01:45:59 -0700933 if (value)
934 val |= bit;
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000935 smc501_writel(val, regs);
Ben Dooksf61be272008-07-25 01:45:59 -0700936
937 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
Ben Dooks98325f82009-01-10 16:59:53 +0100938 sm501_gpio_ensure_gpio(smchip, bit);
939
Ben Dooksf61be272008-07-25 01:45:59 -0700940 spin_unlock_irqrestore(&smgpio->lock, save);
941}
942
943static int sm501_gpio_input(struct gpio_chip *chip, unsigned offset)
944{
Linus Walleij3a504102016-03-30 10:48:05 +0200945 struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700946 struct sm501_gpio *smgpio = smchip->ourgpio;
947 void __iomem *regs = smchip->regbase;
948 unsigned long bit = 1 << offset;
949 unsigned long save;
950 unsigned long ddr;
951
Ben Dooks98325f82009-01-10 16:59:53 +0100952 dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
953 __func__, chip, offset);
Ben Dooksf61be272008-07-25 01:45:59 -0700954
955 spin_lock_irqsave(&smgpio->lock, save);
956
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000957 ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
958 smc501_writel(ddr & ~bit, regs + SM501_GPIO_DDR_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700959
960 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
Ben Dooks98325f82009-01-10 16:59:53 +0100961 sm501_gpio_ensure_gpio(smchip, bit);
962
Ben Dooksf61be272008-07-25 01:45:59 -0700963 spin_unlock_irqrestore(&smgpio->lock, save);
964
965 return 0;
966}
967
968static int sm501_gpio_output(struct gpio_chip *chip,
969 unsigned offset, int value)
970{
Linus Walleij3a504102016-03-30 10:48:05 +0200971 struct sm501_gpio_chip *smchip = gpiochip_get_data(chip);
Ben Dooksf61be272008-07-25 01:45:59 -0700972 struct sm501_gpio *smgpio = smchip->ourgpio;
973 unsigned long bit = 1 << offset;
974 void __iomem *regs = smchip->regbase;
975 unsigned long save;
976 unsigned long val;
977 unsigned long ddr;
978
979 dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d,%d)\n",
980 __func__, chip, offset, value);
981
982 spin_lock_irqsave(&smgpio->lock, save);
983
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000984 val = smc501_readl(regs + SM501_GPIO_DATA_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700985 if (value)
986 val |= bit;
987 else
988 val &= ~bit;
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000989 smc501_writel(val, regs);
Ben Dooksf61be272008-07-25 01:45:59 -0700990
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000991 ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
992 smc501_writel(ddr | bit, regs + SM501_GPIO_DDR_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700993
994 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
Heiko Schocherbf5f0012011-01-24 09:57:20 +0000995 smc501_writel(val, regs + SM501_GPIO_DATA_LOW);
Ben Dooksf61be272008-07-25 01:45:59 -0700996
997 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
998 spin_unlock_irqrestore(&smgpio->lock, save);
999
1000 return 0;
1001}
1002
Julia Lawall7e94e512016-09-11 14:14:38 +02001003static const struct gpio_chip gpio_chip_template = {
Ben Dooksf61be272008-07-25 01:45:59 -07001004 .ngpio = 32,
1005 .direction_input = sm501_gpio_input,
1006 .direction_output = sm501_gpio_output,
1007 .set = sm501_gpio_set,
1008 .get = sm501_gpio_get,
1009};
1010
Bill Pembertonf791be42012-11-19 13:23:04 -05001011static int sm501_gpio_register_chip(struct sm501_devdata *sm,
Ben Dooksf61be272008-07-25 01:45:59 -07001012 struct sm501_gpio *gpio,
1013 struct sm501_gpio_chip *chip)
1014{
1015 struct sm501_platdata *pdata = sm->platdata;
1016 struct gpio_chip *gchip = &chip->gpio;
Arnaud Patard60e540d2008-07-25 01:46:00 -07001017 int base = pdata->gpio_base;
Ben Dooksf61be272008-07-25 01:45:59 -07001018
Ben Dooks28130be2008-07-25 01:46:02 -07001019 chip->gpio = gpio_chip_template;
Ben Dooksf61be272008-07-25 01:45:59 -07001020
1021 if (chip == &gpio->high) {
Arnaud Patard60e540d2008-07-25 01:46:00 -07001022 if (base > 0)
1023 base += 32;
Ben Dooksf61be272008-07-25 01:45:59 -07001024 chip->regbase = gpio->regs + SM501_GPIO_DATA_HIGH;
Ben Dooks98325f82009-01-10 16:59:53 +01001025 chip->control = sm->regs + SM501_GPIO63_32_CONTROL;
Ben Dooksf61be272008-07-25 01:45:59 -07001026 gchip->label = "SM501-HIGH";
1027 } else {
1028 chip->regbase = gpio->regs + SM501_GPIO_DATA_LOW;
Ben Dooks98325f82009-01-10 16:59:53 +01001029 chip->control = sm->regs + SM501_GPIO31_0_CONTROL;
Ben Dooksf61be272008-07-25 01:45:59 -07001030 gchip->label = "SM501-LOW";
1031 }
1032
1033 gchip->base = base;
1034 chip->ourgpio = gpio;
1035
Linus Walleij3a504102016-03-30 10:48:05 +02001036 return gpiochip_add_data(gchip, chip);
Ben Dooksf61be272008-07-25 01:45:59 -07001037}
1038
Bill Pembertonf791be42012-11-19 13:23:04 -05001039static int sm501_register_gpio(struct sm501_devdata *sm)
Ben Dooksf61be272008-07-25 01:45:59 -07001040{
1041 struct sm501_gpio *gpio = &sm->gpio;
1042 resource_size_t iobase = sm->io_res->start + SM501_GPIO;
1043 int ret;
Ben Dooksf61be272008-07-25 01:45:59 -07001044
1045 dev_dbg(sm->dev, "registering gpio block %08llx\n",
1046 (unsigned long long)iobase);
1047
1048 spin_lock_init(&gpio->lock);
1049
1050 gpio->regs_res = request_mem_region(iobase, 0x20, "sm501-gpio");
Markus Elfring42021512018-03-08 14:20:06 +01001051 if (!gpio->regs_res) {
Ben Dooksf61be272008-07-25 01:45:59 -07001052 dev_err(sm->dev, "gpio: failed to request region\n");
1053 return -ENXIO;
1054 }
1055
1056 gpio->regs = ioremap(iobase, 0x20);
Markus Elfring42021512018-03-08 14:20:06 +01001057 if (!gpio->regs) {
Ben Dooksf61be272008-07-25 01:45:59 -07001058 dev_err(sm->dev, "gpio: failed to remap registers\n");
1059 ret = -ENXIO;
Ben Dooks28130be2008-07-25 01:46:02 -07001060 goto err_claimed;
Ben Dooksf61be272008-07-25 01:45:59 -07001061 }
1062
1063 /* Register both our chips. */
1064
1065 ret = sm501_gpio_register_chip(sm, gpio, &gpio->low);
1066 if (ret) {
1067 dev_err(sm->dev, "failed to add low chip\n");
1068 goto err_mapped;
1069 }
1070
1071 ret = sm501_gpio_register_chip(sm, gpio, &gpio->high);
1072 if (ret) {
1073 dev_err(sm->dev, "failed to add high chip\n");
1074 goto err_low_chip;
1075 }
1076
1077 gpio->registered = 1;
1078
1079 return 0;
1080
1081 err_low_chip:
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001082 gpiochip_remove(&gpio->low.gpio);
Ben Dooksf61be272008-07-25 01:45:59 -07001083
1084 err_mapped:
Ben Dooks28130be2008-07-25 01:46:02 -07001085 iounmap(gpio->regs);
1086
1087 err_claimed:
Ben Dooksf61be272008-07-25 01:45:59 -07001088 release_resource(gpio->regs_res);
1089 kfree(gpio->regs_res);
1090
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;
Ben Dooksf61be272008-07-25 01:45:59 -07001097
Ben Dooksf2999202008-07-25 01:46:02 -07001098 if (!sm->gpio.registered)
1099 return;
1100
abdoulaye berthe88d5e522014-07-12 22:30:14 +02001101 gpiochip_remove(&gpio->low.gpio);
1102 gpiochip_remove(&gpio->high.gpio);
Ben Dooks28130be2008-07-25 01:46:02 -07001103
1104 iounmap(gpio->regs);
1105 release_resource(gpio->regs_res);
1106 kfree(gpio->regs_res);
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:
1429 release_resource(sm->regs_claim);
1430 kfree(sm->regs_claim);
1431 err_res:
1432 kfree(sm);
1433 err1:
Roel Kluin7cf52442008-10-22 01:39:55 +02001434 return ret;
Ben Dooksb6d64542007-02-20 13:58:01 -08001435
1436}
1437
Ben Dooks331d7472007-06-23 17:16:28 -07001438#ifdef CONFIG_PM
Ben Dooks472dba72008-07-25 01:45:58 -07001439
Ben Dooks331d7472007-06-23 17:16:28 -07001440/* power management support */
1441
Ben Dooks472dba72008-07-25 01:45:58 -07001442static void sm501_set_power(struct sm501_devdata *sm, int on)
1443{
1444 struct sm501_platdata *pd = sm->platdata;
1445
Markus Elfring42021512018-03-08 14:20:06 +01001446 if (!pd)
Ben Dooks472dba72008-07-25 01:45:58 -07001447 return;
1448
1449 if (pd->get_power) {
1450 if (pd->get_power(sm->dev) == on) {
1451 dev_dbg(sm->dev, "is already %d\n", on);
1452 return;
1453 }
1454 }
1455
1456 if (pd->set_power) {
1457 dev_dbg(sm->dev, "setting power to %d\n", on);
1458
1459 pd->set_power(sm->dev, on);
1460 sm501_mdelay(sm, 10);
1461 }
1462}
1463
Ben Dooks331d7472007-06-23 17:16:28 -07001464static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
1465{
1466 struct sm501_devdata *sm = platform_get_drvdata(pdev);
1467
1468 sm->in_suspend = 1;
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001469 sm->pm_misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
Ben Dooks331d7472007-06-23 17:16:28 -07001470
1471 sm501_dump_regs(sm);
Ben Dooks472dba72008-07-25 01:45:58 -07001472
1473 if (sm->platdata) {
1474 if (sm->platdata->flags & SM501_FLAG_SUSPEND_OFF)
1475 sm501_set_power(sm, 0);
1476 }
1477
Ben Dooks331d7472007-06-23 17:16:28 -07001478 return 0;
1479}
1480
1481static int sm501_plat_resume(struct platform_device *pdev)
1482{
1483 struct sm501_devdata *sm = platform_get_drvdata(pdev);
1484
Ben Dooks472dba72008-07-25 01:45:58 -07001485 sm501_set_power(sm, 1);
1486
Ben Dooks331d7472007-06-23 17:16:28 -07001487 sm501_dump_regs(sm);
1488 sm501_dump_gate(sm);
1489 sm501_dump_clk(sm);
1490
1491 /* check to see if we are in the same state as when suspended */
1492
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001493 if (smc501_readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
Ben Dooks331d7472007-06-23 17:16:28 -07001494 dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
Heiko Schocherbf5f0012011-01-24 09:57:20 +00001495 smc501_writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
Ben Dooks331d7472007-06-23 17:16:28 -07001496
1497 /* our suspend causes the controller state to change,
1498 * either by something attempting setup, power loss,
1499 * or an external reset event on power change */
1500
1501 if (sm->platdata && sm->platdata->init) {
1502 sm501_init_regs(sm, sm->platdata->init);
1503 }
1504 }
1505
1506 /* dump our state from resume */
1507
1508 sm501_dump_regs(sm);
1509 sm501_dump_clk(sm);
1510
1511 sm->in_suspend = 0;
1512
1513 return 0;
1514}
1515#else
1516#define sm501_plat_suspend NULL
1517#define sm501_plat_resume NULL
1518#endif
1519
Ben Dooksb6d64542007-02-20 13:58:01 -08001520/* Initialisation data for PCI devices */
1521
1522static struct sm501_initdata sm501_pci_initdata = {
1523 .gpio_high = {
1524 .set = 0x3F000000, /* 24bit panel */
1525 .mask = 0x0,
1526 },
1527 .misc_timing = {
1528 .set = 0x010100, /* SDRAM timing */
1529 .mask = 0x1F1F00,
1530 },
1531 .misc_control = {
1532 .set = SM501_MISC_PNL_24BIT,
1533 .mask = 0,
1534 },
1535
1536 .devices = SM501_USE_ALL,
Ben Dooks81906222007-06-23 17:16:30 -07001537
1538 /* Errata AB-3 says that 72MHz is the fastest available
1539 * for 33MHZ PCI with proper bus-mastering operation */
1540
1541 .mclk = 72 * MHZ,
1542 .m1xclk = 144 * MHZ,
Ben Dooksb6d64542007-02-20 13:58:01 -08001543};
1544
1545static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
1546 .flags = (SM501FB_FLAG_USE_INIT_MODE |
1547 SM501FB_FLAG_USE_HWCURSOR |
1548 SM501FB_FLAG_USE_HWACCEL |
1549 SM501FB_FLAG_DISABLE_AT_EXIT),
1550};
1551
1552static struct sm501_platdata_fb sm501_fb_pdata = {
1553 .fb_route = SM501_FB_OWN,
1554 .fb_crt = &sm501_pdata_fbsub,
1555 .fb_pnl = &sm501_pdata_fbsub,
1556};
1557
1558static struct sm501_platdata sm501_pci_platdata = {
1559 .init = &sm501_pci_initdata,
1560 .fb = &sm501_fb_pdata,
Arnaud Patard60e540d2008-07-25 01:46:00 -07001561 .gpio_base = -1,
Ben Dooksb6d64542007-02-20 13:58:01 -08001562};
1563
Bill Pembertonf791be42012-11-19 13:23:04 -05001564static int sm501_pci_probe(struct pci_dev *dev,
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001565 const struct pci_device_id *id)
Ben Dooksb6d64542007-02-20 13:58:01 -08001566{
1567 struct sm501_devdata *sm;
1568 int err;
1569
Markus Elfring3eec4fa2018-03-08 14:05:41 +01001570 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
Markus Elfring42021512018-03-08 14:20:06 +01001571 if (!sm) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001572 err = -ENOMEM;
1573 goto err1;
1574 }
1575
1576 /* set a default set of platform data */
1577 dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
1578
1579 /* set a hopefully unique id for our child platform devices */
1580 sm->pdev_id = 32 + dev->devfn;
1581
1582 pci_set_drvdata(dev, sm);
1583
1584 err = pci_enable_device(dev);
1585 if (err) {
1586 dev_err(&dev->dev, "cannot enable device\n");
1587 goto err2;
1588 }
1589
1590 sm->dev = &dev->dev;
1591 sm->irq = dev->irq;
1592
1593#ifdef __BIG_ENDIAN
1594 /* if the system is big-endian, we most probably have a
1595 * translation in the IO layer making the PCI bus little endian
1596 * so make the framebuffer swapped pixels */
1597
1598 sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
1599#endif
1600
1601 /* check our resources */
1602
1603 if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
1604 dev_err(&dev->dev, "region #0 is not memory?\n");
1605 err = -EINVAL;
1606 goto err3;
1607 }
1608
1609 if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
1610 dev_err(&dev->dev, "region #1 is not memory?\n");
1611 err = -EINVAL;
1612 goto err3;
1613 }
1614
1615 /* make our resources ready for sharing */
1616
1617 sm->io_res = &dev->resource[1];
1618 sm->mem_res = &dev->resource[0];
1619
1620 sm->regs_claim = request_mem_region(sm->io_res->start,
Samuel Ortiza5300dc2010-03-08 20:07:48 +01001621 0x100, "sm501");
Markus Elfring42021512018-03-08 14:20:06 +01001622 if (!sm->regs_claim) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001623 dev_err(&dev->dev, "cannot claim registers\n");
1624 err= -EBUSY;
1625 goto err3;
1626 }
1627
Arjan van de Ven7ab18992008-09-28 16:14:52 -07001628 sm->regs = pci_ioremap_bar(dev, 1);
Markus Elfring42021512018-03-08 14:20:06 +01001629 if (!sm->regs) {
Ben Dooksb6d64542007-02-20 13:58:01 -08001630 dev_err(&dev->dev, "cannot remap registers\n");
1631 err = -EIO;
1632 goto err4;
1633 }
1634
1635 sm501_init_dev(sm);
1636 return 0;
1637
1638 err4:
1639 release_resource(sm->regs_claim);
1640 kfree(sm->regs_claim);
1641 err3:
1642 pci_disable_device(dev);
1643 err2:
Ben Dooksb6d64542007-02-20 13:58:01 -08001644 kfree(sm);
1645 err1:
1646 return err;
1647}
1648
1649static void sm501_remove_sub(struct sm501_devdata *sm,
1650 struct sm501_device *smdev)
1651{
1652 list_del(&smdev->list);
1653 platform_device_unregister(&smdev->pdev);
1654}
1655
1656static void sm501_dev_remove(struct sm501_devdata *sm)
1657{
1658 struct sm501_device *smdev, *tmp;
1659
1660 list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
1661 sm501_remove_sub(sm, smdev);
1662
1663 device_remove_file(sm->dev, &dev_attr_dbg_regs);
Ben Dooksf61be272008-07-25 01:45:59 -07001664
Ben Dooksf2999202008-07-25 01:46:02 -07001665 sm501_gpio_remove(sm);
Ben Dooksb6d64542007-02-20 13:58:01 -08001666}
1667
Bill Pemberton4740f732012-11-19 13:26:01 -05001668static void sm501_pci_remove(struct pci_dev *dev)
Ben Dooksb6d64542007-02-20 13:58:01 -08001669{
1670 struct sm501_devdata *sm = pci_get_drvdata(dev);
1671
1672 sm501_dev_remove(sm);
1673 iounmap(sm->regs);
1674
1675 release_resource(sm->regs_claim);
1676 kfree(sm->regs_claim);
1677
Ben Dooksb6d64542007-02-20 13:58:01 -08001678 pci_disable_device(dev);
1679}
1680
1681static int sm501_plat_remove(struct platform_device *dev)
1682{
1683 struct sm501_devdata *sm = platform_get_drvdata(dev);
1684
1685 sm501_dev_remove(sm);
1686 iounmap(sm->regs);
1687
1688 release_resource(sm->regs_claim);
1689 kfree(sm->regs_claim);
1690
1691 return 0;
1692}
1693
Jingoo Han36fcd062013-12-03 08:15:39 +09001694static const struct pci_device_id sm501_pci_tbl[] = {
Ben Dooksb6d64542007-02-20 13:58:01 -08001695 { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1696 { 0, },
1697};
1698
1699MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
1700
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001701static struct pci_driver sm501_pci_driver = {
Ben Dooksb6d64542007-02-20 13:58:01 -08001702 .name = "sm501",
1703 .id_table = sm501_pci_tbl,
1704 .probe = sm501_pci_probe,
Bill Pemberton84449212012-11-19 13:20:24 -05001705 .remove = sm501_pci_remove,
Ben Dooksb6d64542007-02-20 13:58:01 -08001706};
1707
Kay Sievers4f46d6e2008-07-25 01:45:47 -07001708MODULE_ALIAS("platform:sm501");
1709
Jingoo Hanae6eee3c2014-05-07 17:24:05 +09001710static const struct of_device_id of_sm501_match_tbl[] = {
Heiko Schocher4295f9b2011-01-26 07:21:30 +00001711 { .compatible = "smi,sm501", },
1712 { /* end */ }
1713};
Luis de Bethencourt327cc182015-09-17 20:16:50 +02001714MODULE_DEVICE_TABLE(of, of_sm501_match_tbl);
Heiko Schocher4295f9b2011-01-26 07:21:30 +00001715
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001716static struct platform_driver sm501_plat_driver = {
Ben Dooksb6d64542007-02-20 13:58:01 -08001717 .driver = {
1718 .name = "sm501",
Heiko Schocher4295f9b2011-01-26 07:21:30 +00001719 .of_match_table = of_sm501_match_tbl,
Ben Dooksb6d64542007-02-20 13:58:01 -08001720 },
1721 .probe = sm501_plat_probe,
1722 .remove = sm501_plat_remove,
Ben Dooks331d7472007-06-23 17:16:28 -07001723 .suspend = sm501_plat_suspend,
1724 .resume = sm501_plat_resume,
Ben Dooksb6d64542007-02-20 13:58:01 -08001725};
1726
1727static int __init sm501_base_init(void)
1728{
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001729 platform_driver_register(&sm501_plat_driver);
1730 return pci_register_driver(&sm501_pci_driver);
Ben Dooksb6d64542007-02-20 13:58:01 -08001731}
1732
1733static void __exit sm501_base_exit(void)
1734{
Alexey Dobriyan158abca2009-02-17 09:10:19 +01001735 platform_driver_unregister(&sm501_plat_driver);
1736 pci_unregister_driver(&sm501_pci_driver);
Ben Dooksb6d64542007-02-20 13:58:01 -08001737}
1738
1739module_init(sm501_base_init);
1740module_exit(sm501_base_exit);
1741
1742MODULE_DESCRIPTION("SM501 Core Driver");
1743MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
1744MODULE_LICENSE("GPL v2");