Merge branch 'x86-mrst-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / arch / arm / common / locomo.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/arm/common/locomo.c
3 *
4 * Sharp LoCoMo support
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This file contains all generic LoCoMo support.
11 *
12 * All initialization functions provided here are intended to be called
13 * from machine specific code with proper arguments when required.
14 *
15 * Based on sa1111.c
16 */
17
1da177e4
LT
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/delay.h>
22#include <linux/errno.h>
23#include <linux/ioport.h>
d052d1be 24#include <linux/platform_device.h>
1da177e4
LT
25#include <linux/slab.h>
26#include <linux/spinlock.h>
fced80c7 27#include <linux/io.h>
1da177e4 28
a09e64fb 29#include <mach/hardware.h>
1da177e4
LT
30#include <asm/irq.h>
31#include <asm/mach/irq.h>
32
33#include <asm/hardware/locomo.h>
34
35/* M62332 output channel selection */
36#define M62332_EVR_CH 1 /* M62332 volume channel number */
37 /* 0 : CH.1 , 1 : CH. 2 */
38/* DAC send data */
39#define M62332_SLAVE_ADDR 0x4e /* Slave address */
40#define M62332_W_BIT 0x00 /* W bit (0 only) */
41#define M62332_SUB_ADDR 0x00 /* Sub address */
42#define M62332_A_BIT 0x00 /* A bit (0 only) */
43
44/* DAC setup and hold times (expressed in us) */
45#define DAC_BUS_FREE_TIME 5 /* 4.7 us */
46#define DAC_START_SETUP_TIME 5 /* 4.7 us */
47#define DAC_STOP_SETUP_TIME 4 /* 4.0 us */
48#define DAC_START_HOLD_TIME 5 /* 4.7 us */
49#define DAC_SCL_LOW_HOLD_TIME 5 /* 4.7 us */
50#define DAC_SCL_HIGH_HOLD_TIME 4 /* 4.0 us */
51#define DAC_DATA_SETUP_TIME 1 /* 250 ns */
52#define DAC_DATA_HOLD_TIME 1 /* 300 ns */
53#define DAC_LOW_SETUP_TIME 1 /* 300 ns */
54#define DAC_HIGH_SETUP_TIME 1 /* 1000 ns */
55
56/* the following is the overall data for the locomo chip */
57struct locomo {
58 struct device *dev;
59 unsigned long phys;
60 unsigned int irq;
61 spinlock_t lock;
54815366 62 void __iomem *base;
93160c63
RW
63#ifdef CONFIG_PM
64 void *saved_state;
65#endif
1da177e4
LT
66};
67
68struct locomo_dev_info {
69 unsigned long offset;
70 unsigned long length;
71 unsigned int devid;
72 unsigned int irq[1];
73 const char * name;
74};
75
76/* All the locomo devices. If offset is non-zero, the mapbase for the
77 * locomo_dev will be set to the chip base plus offset. If offset is
78 * zero, then the mapbase for the locomo_dev will be set to zero. An
79 * offset of zero means the device only uses GPIOs or other helper
80 * functions inside this file */
81static struct locomo_dev_info locomo_devices[] = {
82 {
83 .devid = LOCOMO_DEVID_KEYBOARD,
84 .irq = {
85 IRQ_LOCOMO_KEY,
86 },
87 .name = "locomo-keyboard",
88 .offset = LOCOMO_KEYBOARD,
89 .length = 16,
90 },
91 {
92 .devid = LOCOMO_DEVID_FRONTLIGHT,
93 .irq = {},
94 .name = "locomo-frontlight",
95 .offset = LOCOMO_FRONTLIGHT,
96 .length = 8,
97
98 },
99 {
100 .devid = LOCOMO_DEVID_BACKLIGHT,
101 .irq = {},
102 .name = "locomo-backlight",
103 .offset = LOCOMO_BACKLIGHT,
104 .length = 8,
105 },
106 {
107 .devid = LOCOMO_DEVID_AUDIO,
108 .irq = {},
109 .name = "locomo-audio",
110 .offset = LOCOMO_AUDIO,
111 .length = 4,
112 },
113 {
114 .devid = LOCOMO_DEVID_LED,
115 .irq = {},
116 .name = "locomo-led",
117 .offset = LOCOMO_LED,
118 .length = 8,
119 },
120 {
121 .devid = LOCOMO_DEVID_UART,
122 .irq = {},
123 .name = "locomo-uart",
124 .offset = 0,
125 .length = 0,
126 },
a2025e7f
DO
127 {
128 .devid = LOCOMO_DEVID_SPI,
129 .irq = {},
130 .name = "locomo-spi",
131 .offset = LOCOMO_SPI,
132 .length = 0x30,
133 },
1da177e4
LT
134};
135
136
137/** LoCoMo interrupt handling stuff.
138 * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
139 * that is, there is only one real hardware interrupt
140 * we determine which interrupt it is by reading some IO memory.
141 * We have two levels of expansion, first in the handler for the
142 * hardware interrupt we generate an interrupt
143 * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
144 *
145 * hardware irq reads LOCOMO_ICR & 0x0f00
146 * IRQ_LOCOMO_KEY_BASE
147 * IRQ_LOCOMO_GPIO_BASE
148 * IRQ_LOCOMO_LT_BASE
149 * IRQ_LOCOMO_SPI_BASE
150 * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
151 * IRQ_LOCOMO_KEY
152 * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
153 * IRQ_LOCOMO_GPIO[0-15]
154 * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
155 * IRQ_LOCOMO_LT
156 * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
157 * IRQ_LOCOMO_SPI_RFR
158 * IRQ_LOCOMO_SPI_RFW
159 * IRQ_LOCOMO_SPI_OVRN
160 * IRQ_LOCOMO_SPI_TEND
161 */
162
163#define LOCOMO_IRQ_START (IRQ_LOCOMO_KEY_BASE)
164#define LOCOMO_IRQ_KEY_START (IRQ_LOCOMO_KEY)
165#define LOCOMO_IRQ_GPIO_START (IRQ_LOCOMO_GPIO0)
166#define LOCOMO_IRQ_LT_START (IRQ_LOCOMO_LT)
167#define LOCOMO_IRQ_SPI_START (IRQ_LOCOMO_SPI_RFR)
168
10dd5ce2 169static void locomo_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
170{
171 int req, i;
10dd5ce2 172 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
173
174 /* Acknowledge the parent IRQ */
175 desc->chip->ack(irq);
176
177 /* check why this interrupt was generated */
178 req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
179
180 if (req) {
181 /* generate the next interrupt(s) */
182 irq = LOCOMO_IRQ_START;
d8aa0251 183 for (i = 0; i <= 3; i++, irq++) {
1da177e4 184 if (req & (0x0100 << i)) {
d8aa0251 185 generic_handle_irq(irq);
1da177e4
LT
186 }
187
188 }
189 }
190}
191
192static void locomo_ack_irq(unsigned int irq)
193{
194}
195
196static void locomo_mask_irq(unsigned int irq)
197{
10dd5ce2 198 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
199 unsigned int r;
200 r = locomo_readl(mapbase + LOCOMO_ICR);
201 r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
202 locomo_writel(r, mapbase + LOCOMO_ICR);
203}
204
205static void locomo_unmask_irq(unsigned int irq)
206{
10dd5ce2 207 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
208 unsigned int r;
209 r = locomo_readl(mapbase + LOCOMO_ICR);
210 r |= (0x0010 << (irq - LOCOMO_IRQ_START));
211 locomo_writel(r, mapbase + LOCOMO_ICR);
212}
213
38c677cb
DB
214static struct irq_chip locomo_chip = {
215 .name = "LOCOMO",
1da177e4
LT
216 .ack = locomo_ack_irq,
217 .mask = locomo_mask_irq,
218 .unmask = locomo_unmask_irq,
219};
220
10dd5ce2 221static void locomo_key_handler(unsigned int irq, struct irq_desc *desc)
1da177e4 222{
10dd5ce2 223 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
224
225 if (locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC) & 0x0001) {
d8aa0251 226 generic_handle_irq(LOCOMO_IRQ_KEY_START);
1da177e4
LT
227 }
228}
229
230static void locomo_key_ack_irq(unsigned int irq)
231{
10dd5ce2 232 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
233 unsigned int r;
234 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
235 r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
236 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
237}
238
239static void locomo_key_mask_irq(unsigned int irq)
240{
10dd5ce2 241 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
242 unsigned int r;
243 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
244 r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
245 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
246}
247
248static void locomo_key_unmask_irq(unsigned int irq)
249{
10dd5ce2 250 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
251 unsigned int r;
252 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
253 r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
254 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
255}
256
38c677cb
DB
257static struct irq_chip locomo_key_chip = {
258 .name = "LOCOMO-key",
1da177e4
LT
259 .ack = locomo_key_ack_irq,
260 .mask = locomo_key_mask_irq,
261 .unmask = locomo_key_unmask_irq,
262};
263
10dd5ce2 264static void locomo_gpio_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
265{
266 int req, i;
10dd5ce2 267 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
268
269 req = locomo_readl(mapbase + LOCOMO_GIR) &
270 locomo_readl(mapbase + LOCOMO_GPD) &
271 0xffff;
272
273 if (req) {
274 irq = LOCOMO_IRQ_GPIO_START;
d8aa0251 275 for (i = 0; i <= 15; i++, irq++) {
1da177e4 276 if (req & (0x0001 << i)) {
d8aa0251 277 generic_handle_irq(irq);
1da177e4
LT
278 }
279 }
280 }
281}
282
283static void locomo_gpio_ack_irq(unsigned int irq)
284{
10dd5ce2 285 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
286 unsigned int r;
287 r = locomo_readl(mapbase + LOCOMO_GWE);
288 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
289 locomo_writel(r, mapbase + LOCOMO_GWE);
290
291 r = locomo_readl(mapbase + LOCOMO_GIS);
292 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
293 locomo_writel(r, mapbase + LOCOMO_GIS);
294
295 r = locomo_readl(mapbase + LOCOMO_GWE);
296 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
297 locomo_writel(r, mapbase + LOCOMO_GWE);
298}
299
300static void locomo_gpio_mask_irq(unsigned int irq)
301{
10dd5ce2 302 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
303 unsigned int r;
304 r = locomo_readl(mapbase + LOCOMO_GIE);
305 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
306 locomo_writel(r, mapbase + LOCOMO_GIE);
307}
308
309static void locomo_gpio_unmask_irq(unsigned int irq)
310{
10dd5ce2 311 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
312 unsigned int r;
313 r = locomo_readl(mapbase + LOCOMO_GIE);
314 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
315 locomo_writel(r, mapbase + LOCOMO_GIE);
316}
317
2a52efb2
TK
318static int GPIO_IRQ_rising_edge;
319static int GPIO_IRQ_falling_edge;
320
321static int locomo_gpio_type(unsigned int irq, unsigned int type)
322{
323 unsigned int mask;
324 void __iomem *mapbase = get_irq_chip_data(irq);
325
326 mask = 1 << (irq - LOCOMO_IRQ_GPIO_START);
327
6cab4860 328 if (type == IRQ_TYPE_PROBE) {
2a52efb2
TK
329 if ((GPIO_IRQ_rising_edge | GPIO_IRQ_falling_edge) & mask)
330 return 0;
6cab4860 331 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
2a52efb2
TK
332 }
333
6cab4860 334 if (type & IRQ_TYPE_EDGE_RISING)
2a52efb2
TK
335 GPIO_IRQ_rising_edge |= mask;
336 else
337 GPIO_IRQ_rising_edge &= ~mask;
6cab4860 338 if (type & IRQ_TYPE_EDGE_FALLING)
2a52efb2
TK
339 GPIO_IRQ_falling_edge |= mask;
340 else
341 GPIO_IRQ_falling_edge &= ~mask;
342 locomo_writel(GPIO_IRQ_rising_edge, mapbase + LOCOMO_GRIE);
343 locomo_writel(GPIO_IRQ_falling_edge, mapbase + LOCOMO_GFIE);
344
345 return 0;
346}
347
38c677cb 348static struct irq_chip locomo_gpio_chip = {
2a52efb2
TK
349 .name = "LOCOMO-gpio",
350 .ack = locomo_gpio_ack_irq,
351 .mask = locomo_gpio_mask_irq,
352 .unmask = locomo_gpio_unmask_irq,
353 .set_type = locomo_gpio_type,
1da177e4
LT
354};
355
10dd5ce2 356static void locomo_lt_handler(unsigned int irq, struct irq_desc *desc)
1da177e4 357{
10dd5ce2 358 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
359
360 if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
d8aa0251 361 generic_handle_irq(LOCOMO_IRQ_LT_START);
1da177e4
LT
362 }
363}
364
365static void locomo_lt_ack_irq(unsigned int irq)
366{
10dd5ce2 367 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
368 unsigned int r;
369 r = locomo_readl(mapbase + LOCOMO_LTINT);
370 r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
371 locomo_writel(r, mapbase + LOCOMO_LTINT);
372}
373
374static void locomo_lt_mask_irq(unsigned int irq)
375{
10dd5ce2 376 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
377 unsigned int r;
378 r = locomo_readl(mapbase + LOCOMO_LTINT);
379 r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
380 locomo_writel(r, mapbase + LOCOMO_LTINT);
381}
382
383static void locomo_lt_unmask_irq(unsigned int irq)
384{
10dd5ce2 385 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4
LT
386 unsigned int r;
387 r = locomo_readl(mapbase + LOCOMO_LTINT);
388 r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
389 locomo_writel(r, mapbase + LOCOMO_LTINT);
390}
391
38c677cb
DB
392static struct irq_chip locomo_lt_chip = {
393 .name = "LOCOMO-lt",
1da177e4
LT
394 .ack = locomo_lt_ack_irq,
395 .mask = locomo_lt_mask_irq,
396 .unmask = locomo_lt_unmask_irq,
397};
398
10dd5ce2 399static void locomo_spi_handler(unsigned int irq, struct irq_desc *desc)
1da177e4
LT
400{
401 int req, i;
10dd5ce2 402 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4 403
a2025e7f 404 req = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIR) & 0x000F;
1da177e4
LT
405 if (req) {
406 irq = LOCOMO_IRQ_SPI_START;
1da177e4 407
d8aa0251 408 for (i = 0; i <= 3; i++, irq++) {
1da177e4 409 if (req & (0x0001 << i)) {
d8aa0251 410 generic_handle_irq(irq);
1da177e4
LT
411 }
412 }
413 }
414}
415
416static void locomo_spi_ack_irq(unsigned int irq)
417{
10dd5ce2 418 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4 419 unsigned int r;
a2025e7f 420 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
1da177e4 421 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
a2025e7f 422 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
1da177e4 423
a2025e7f 424 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
1da177e4 425 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
a2025e7f 426 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
1da177e4 427
a2025e7f 428 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
1da177e4 429 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
a2025e7f 430 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
1da177e4
LT
431}
432
433static void locomo_spi_mask_irq(unsigned int irq)
434{
10dd5ce2 435 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4 436 unsigned int r;
a2025e7f 437 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
1da177e4 438 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
a2025e7f 439 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
1da177e4
LT
440}
441
442static void locomo_spi_unmask_irq(unsigned int irq)
443{
10dd5ce2 444 void __iomem *mapbase = get_irq_chip_data(irq);
1da177e4 445 unsigned int r;
a2025e7f 446 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
1da177e4 447 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
a2025e7f 448 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
1da177e4
LT
449}
450
38c677cb
DB
451static struct irq_chip locomo_spi_chip = {
452 .name = "LOCOMO-spi",
1da177e4
LT
453 .ack = locomo_spi_ack_irq,
454 .mask = locomo_spi_mask_irq,
455 .unmask = locomo_spi_unmask_irq,
456};
457
458static void locomo_setup_irq(struct locomo *lchip)
459{
460 int irq;
54815366 461 void __iomem *irqbase = lchip->base;
1da177e4
LT
462
463 /*
464 * Install handler for IRQ_LOCOMO_HW.
465 */
6cab4860 466 set_irq_type(lchip->irq, IRQ_TYPE_EDGE_FALLING);
10dd5ce2 467 set_irq_chip_data(lchip->irq, irqbase);
1da177e4
LT
468 set_irq_chained_handler(lchip->irq, locomo_handler);
469
470 /* Install handlers for IRQ_LOCOMO_*_BASE */
471 set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
10dd5ce2 472 set_irq_chip_data(IRQ_LOCOMO_KEY_BASE, irqbase);
1da177e4 473 set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
1da177e4
LT
474
475 set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
10dd5ce2 476 set_irq_chip_data(IRQ_LOCOMO_GPIO_BASE, irqbase);
1da177e4 477 set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
1da177e4
LT
478
479 set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
10dd5ce2 480 set_irq_chip_data(IRQ_LOCOMO_LT_BASE, irqbase);
1da177e4 481 set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
1da177e4
LT
482
483 set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
10dd5ce2 484 set_irq_chip_data(IRQ_LOCOMO_SPI_BASE, irqbase);
1da177e4 485 set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
1da177e4
LT
486
487 /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
488 set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
10dd5ce2
RK
489 set_irq_chip_data(LOCOMO_IRQ_KEY_START, irqbase);
490 set_irq_handler(LOCOMO_IRQ_KEY_START, handle_edge_irq);
1da177e4
LT
491 set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
492
493 /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
494 for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
495 set_irq_chip(irq, &locomo_gpio_chip);
10dd5ce2
RK
496 set_irq_chip_data(irq, irqbase);
497 set_irq_handler(irq, handle_edge_irq);
1da177e4
LT
498 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
499 }
500
501 /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
502 set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
10dd5ce2
RK
503 set_irq_chip_data(LOCOMO_IRQ_LT_START, irqbase);
504 set_irq_handler(LOCOMO_IRQ_LT_START, handle_edge_irq);
1da177e4
LT
505 set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
506
507 /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
2a52efb2 508 for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 4; irq++) {
1da177e4 509 set_irq_chip(irq, &locomo_spi_chip);
10dd5ce2
RK
510 set_irq_chip_data(irq, irqbase);
511 set_irq_handler(irq, handle_edge_irq);
1da177e4
LT
512 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
513 }
514}
515
516
517static void locomo_dev_release(struct device *_dev)
518{
519 struct locomo_dev *dev = LOCOMO_DEV(_dev);
520
521 kfree(dev);
522}
523
524static int
525locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
526{
527 struct locomo_dev *dev;
528 int ret;
529
d2a02b93 530 dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
1da177e4
LT
531 if (!dev) {
532 ret = -ENOMEM;
533 goto out;
534 }
1da177e4 535
1da177e4
LT
536 /*
537 * If the parent device has a DMA mask associated with it,
538 * propagate it down to the children.
539 */
540 if (lchip->dev->dma_mask) {
541 dev->dma_mask = *lchip->dev->dma_mask;
542 dev->dev.dma_mask = &dev->dma_mask;
543 }
544
3f978704 545 dev_set_name(&dev->dev, "%s", info->name);
1da177e4
LT
546 dev->devid = info->devid;
547 dev->dev.parent = lchip->dev;
548 dev->dev.bus = &locomo_bus_type;
549 dev->dev.release = locomo_dev_release;
550 dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
551
552 if (info->offset)
553 dev->mapbase = lchip->base + info->offset;
554 else
555 dev->mapbase = 0;
556 dev->length = info->length;
557
558 memmove(dev->irq, info->irq, sizeof(dev->irq));
559
560 ret = device_register(&dev->dev);
561 if (ret) {
562 out:
563 kfree(dev);
564 }
565 return ret;
566}
567
b38d950d
JL
568#ifdef CONFIG_PM
569
570struct locomo_save_data {
571 u16 LCM_GPO;
572 u16 LCM_SPICT;
573 u16 LCM_GPE;
574 u16 LCM_ASD;
575 u16 LCM_SPIMD;
576};
577
3ae5eaec 578static int locomo_suspend(struct platform_device *dev, pm_message_t state)
b38d950d 579{
3ae5eaec 580 struct locomo *lchip = platform_get_drvdata(dev);
b38d950d
JL
581 struct locomo_save_data *save;
582 unsigned long flags;
583
b38d950d
JL
584 save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
585 if (!save)
586 return -ENOMEM;
587
93160c63 588 lchip->saved_state = save;
b38d950d
JL
589
590 spin_lock_irqsave(&lchip->lock, flags);
591
592 save->LCM_GPO = locomo_readl(lchip->base + LOCOMO_GPO); /* GPIO */
593 locomo_writel(0x00, lchip->base + LOCOMO_GPO);
2a52efb2 594 save->LCM_SPICT = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPICT); /* SPI */
b38d950d
JL
595 locomo_writel(0x40, lchip->base + LOCOMO_SPICT);
596 save->LCM_GPE = locomo_readl(lchip->base + LOCOMO_GPE); /* GPIO */
597 locomo_writel(0x00, lchip->base + LOCOMO_GPE);
598 save->LCM_ASD = locomo_readl(lchip->base + LOCOMO_ASD); /* ADSTART */
599 locomo_writel(0x00, lchip->base + LOCOMO_ASD);
2a52efb2
TK
600 save->LCM_SPIMD = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPIMD); /* SPI */
601 locomo_writel(0x3C14, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
b38d950d
JL
602
603 locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
604 locomo_writel(0x00, lchip->base + LOCOMO_DAC);
605 locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
606
2a52efb2 607 if ((locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88))
b38d950d
JL
608 locomo_writel(0x00, lchip->base + LOCOMO_C32K); /* CLK32 off */
609 else
610 /* 18MHz already enabled, so no wait */
611 locomo_writel(0xc1, lchip->base + LOCOMO_C32K); /* CLK32 on */
612
613 locomo_writel(0x00, lchip->base + LOCOMO_TADC); /* 18MHz clock off*/
614 locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC); /* 22MHz/24MHz clock off */
615 locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS); /* FL */
616
617 spin_unlock_irqrestore(&lchip->lock, flags);
618
619 return 0;
620}
621
3ae5eaec 622static int locomo_resume(struct platform_device *dev)
b38d950d 623{
3ae5eaec 624 struct locomo *lchip = platform_get_drvdata(dev);
b38d950d
JL
625 struct locomo_save_data *save;
626 unsigned long r;
627 unsigned long flags;
93160c63
RW
628
629 save = lchip->saved_state;
b38d950d
JL
630 if (!save)
631 return 0;
632
633 spin_lock_irqsave(&lchip->lock, flags);
634
635 locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
2a52efb2 636 locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPI + LOCOMO_SPICT);
b38d950d
JL
637 locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
638 locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
2a52efb2 639 locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
b38d950d
JL
640
641 locomo_writel(0x00, lchip->base + LOCOMO_C32K);
642 locomo_writel(0x90, lchip->base + LOCOMO_TADC);
643
644 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
645 r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
646 r &= 0xFEFF;
647 locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
648 locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
649
650 spin_unlock_irqrestore(&lchip->lock, flags);
93160c63
RW
651
652 lchip->saved_state = NULL;
b38d950d
JL
653 kfree(save);
654
655 return 0;
656}
657#endif
658
4ebf2d00 659
1da177e4
LT
660/**
661 * locomo_probe - probe for a single LoCoMo chip.
662 * @phys_addr: physical address of device.
663 *
664 * Probe for a LoCoMo chip. This must be called
665 * before any other locomo-specific code.
666 *
667 * Returns:
668 * %-ENODEV device not found.
669 * %-EBUSY physical address already marked in-use.
670 * %0 successful.
671 */
672static int
673__locomo_probe(struct device *me, struct resource *mem, int irq)
674{
675 struct locomo *lchip;
676 unsigned long r;
677 int i, ret = -ENODEV;
678
d2a02b93 679 lchip = kzalloc(sizeof(struct locomo), GFP_KERNEL);
1da177e4
LT
680 if (!lchip)
681 return -ENOMEM;
682
1da177e4
LT
683 spin_lock_init(&lchip->lock);
684
685 lchip->dev = me;
686 dev_set_drvdata(lchip->dev, lchip);
687
688 lchip->phys = mem->start;
689 lchip->irq = irq;
690
691 /*
692 * Map the whole region. This also maps the
693 * registers for our children.
694 */
695 lchip->base = ioremap(mem->start, PAGE_SIZE);
696 if (!lchip->base) {
697 ret = -ENOMEM;
698 goto out;
699 }
700
701 /* locomo initialize */
702 locomo_writel(0, lchip->base + LOCOMO_ICR);
703 /* KEYBOARD */
704 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
705
706 /* GPIO */
707 locomo_writel(0, lchip->base + LOCOMO_GPO);
2a52efb2 708 locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
1da177e4 709 , lchip->base + LOCOMO_GPE);
2a52efb2 710 locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
1da177e4
LT
711 , lchip->base + LOCOMO_GPD);
712 locomo_writel(0, lchip->base + LOCOMO_GIE);
713
e4423781 714 /* Frontlight */
1da177e4
LT
715 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
716 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
4ebf2d00 717
1da177e4
LT
718 /* Longtime timer */
719 locomo_writel(0, lchip->base + LOCOMO_LTINT);
720 /* SPI */
721 locomo_writel(0, lchip->base + LOCOMO_SPIIE);
722
723 locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
724 r = locomo_readl(lchip->base + LOCOMO_ASD);
725 r |= 0x8000;
726 locomo_writel(r, lchip->base + LOCOMO_ASD);
727
728 locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
729 r = locomo_readl(lchip->base + LOCOMO_HSD);
730 r |= 0x8000;
731 locomo_writel(r, lchip->base + LOCOMO_HSD);
732
733 locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
734
735 /* XON */
736 locomo_writel(0x80, lchip->base + LOCOMO_TADC);
737 udelay(1000);
738 /* CLK9MEN */
739 r = locomo_readl(lchip->base + LOCOMO_TADC);
740 r |= 0x10;
741 locomo_writel(r, lchip->base + LOCOMO_TADC);
742 udelay(100);
743
744 /* init DAC */
745 r = locomo_readl(lchip->base + LOCOMO_DAC);
746 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
747 locomo_writel(r, lchip->base + LOCOMO_DAC);
748
749 r = locomo_readl(lchip->base + LOCOMO_VER);
750 printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
751
752 /*
753 * The interrupt controller must be initialised before any
754 * other device to ensure that the interrupts are available.
755 */
756 if (lchip->irq != NO_IRQ)
757 locomo_setup_irq(lchip);
758
759 for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
760 locomo_init_one_child(lchip, &locomo_devices[i]);
1da177e4
LT
761 return 0;
762
763 out:
764 kfree(lchip);
765 return ret;
766}
767
e24da5d3 768static int locomo_remove_child(struct device *dev, void *data)
1da177e4 769{
e24da5d3
PM
770 device_unregister(dev);
771 return 0;
772}
1da177e4 773
e24da5d3
PM
774static void __locomo_remove(struct locomo *lchip)
775{
776 device_for_each_child(lchip->dev, NULL, locomo_remove_child);
1da177e4
LT
777
778 if (lchip->irq != NO_IRQ) {
779 set_irq_chained_handler(lchip->irq, NULL);
780 set_irq_data(lchip->irq, NULL);
781 }
782
783 iounmap(lchip->base);
784 kfree(lchip);
785}
786
3ae5eaec 787static int locomo_probe(struct platform_device *dev)
1da177e4 788{
1da177e4
LT
789 struct resource *mem;
790 int irq;
791
3ae5eaec 792 mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
1da177e4
LT
793 if (!mem)
794 return -EINVAL;
3ae5eaec 795 irq = platform_get_irq(dev, 0);
48944738
DV
796 if (irq < 0)
797 return -ENXIO;
1da177e4 798
3ae5eaec 799 return __locomo_probe(&dev->dev, mem, irq);
1da177e4
LT
800}
801
3ae5eaec 802static int locomo_remove(struct platform_device *dev)
1da177e4 803{
c35bf4a5 804 struct locomo *lchip = platform_get_drvdata(dev);
1da177e4
LT
805
806 if (lchip) {
807 __locomo_remove(lchip);
3ae5eaec 808 platform_set_drvdata(dev, NULL);
1da177e4
LT
809 }
810
811 return 0;
812}
813
814/*
815 * Not sure if this should be on the system bus or not yet.
816 * We really want some way to register a system device at
817 * the per-machine level, and then have this driver pick
818 * up the registered devices.
819 */
3ae5eaec 820static struct platform_driver locomo_device_driver = {
1da177e4
LT
821 .probe = locomo_probe,
822 .remove = locomo_remove,
b38d950d
JL
823#ifdef CONFIG_PM
824 .suspend = locomo_suspend,
825 .resume = locomo_resume,
826#endif
3ae5eaec
RK
827 .driver = {
828 .name = "locomo",
829 },
1da177e4
LT
830};
831
832/*
833 * Get the parent device driver (us) structure
834 * from a child function device
835 */
836static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
837{
838 return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
839}
840
8d48427e 841void locomo_gpio_set_dir(struct device *dev, unsigned int bits, unsigned int dir)
1da177e4 842{
8d48427e 843 struct locomo *lchip = dev_get_drvdata(dev);
1da177e4
LT
844 unsigned long flags;
845 unsigned int r;
846
8d48427e
RP
847 if (!lchip)
848 return;
849
1da177e4
LT
850 spin_lock_irqsave(&lchip->lock, flags);
851
852 r = locomo_readl(lchip->base + LOCOMO_GPD);
2a52efb2
TK
853 if (dir)
854 r |= bits;
855 else
856 r &= ~bits;
1da177e4
LT
857 locomo_writel(r, lchip->base + LOCOMO_GPD);
858
859 r = locomo_readl(lchip->base + LOCOMO_GPE);
860 if (dir)
861 r |= bits;
862 else
863 r &= ~bits;
864 locomo_writel(r, lchip->base + LOCOMO_GPE);
865
866 spin_unlock_irqrestore(&lchip->lock, flags);
867}
9c5e8fec 868EXPORT_SYMBOL(locomo_gpio_set_dir);
1da177e4 869
8d48427e 870int locomo_gpio_read_level(struct device *dev, unsigned int bits)
1da177e4 871{
8d48427e 872 struct locomo *lchip = dev_get_drvdata(dev);
1da177e4
LT
873 unsigned long flags;
874 unsigned int ret;
875
8d48427e
RP
876 if (!lchip)
877 return -ENODEV;
878
1da177e4
LT
879 spin_lock_irqsave(&lchip->lock, flags);
880 ret = locomo_readl(lchip->base + LOCOMO_GPL);
881 spin_unlock_irqrestore(&lchip->lock, flags);
882
883 ret &= bits;
884 return ret;
885}
9c5e8fec 886EXPORT_SYMBOL(locomo_gpio_read_level);
1da177e4 887
8d48427e 888int locomo_gpio_read_output(struct device *dev, unsigned int bits)
1da177e4 889{
8d48427e 890 struct locomo *lchip = dev_get_drvdata(dev);
1da177e4
LT
891 unsigned long flags;
892 unsigned int ret;
893
8d48427e
RP
894 if (!lchip)
895 return -ENODEV;
896
1da177e4
LT
897 spin_lock_irqsave(&lchip->lock, flags);
898 ret = locomo_readl(lchip->base + LOCOMO_GPO);
899 spin_unlock_irqrestore(&lchip->lock, flags);
900
901 ret &= bits;
902 return ret;
903}
9c5e8fec 904EXPORT_SYMBOL(locomo_gpio_read_output);
1da177e4 905
8d48427e 906void locomo_gpio_write(struct device *dev, unsigned int bits, unsigned int set)
1da177e4 907{
8d48427e 908 struct locomo *lchip = dev_get_drvdata(dev);
1da177e4
LT
909 unsigned long flags;
910 unsigned int r;
911
8d48427e
RP
912 if (!lchip)
913 return;
914
1da177e4
LT
915 spin_lock_irqsave(&lchip->lock, flags);
916
917 r = locomo_readl(lchip->base + LOCOMO_GPO);
918 if (set)
919 r |= bits;
920 else
921 r &= ~bits;
922 locomo_writel(r, lchip->base + LOCOMO_GPO);
923
924 spin_unlock_irqrestore(&lchip->lock, flags);
925}
9c5e8fec 926EXPORT_SYMBOL(locomo_gpio_write);
1da177e4
LT
927
928static void locomo_m62332_sendbit(void *mapbase, int bit)
929{
930 unsigned int r;
931
932 r = locomo_readl(mapbase + LOCOMO_DAC);
933 r &= ~(LOCOMO_DAC_SCLOEB);
934 locomo_writel(r, mapbase + LOCOMO_DAC);
935 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
936 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
937 r = locomo_readl(mapbase + LOCOMO_DAC);
938 r &= ~(LOCOMO_DAC_SCLOEB);
939 locomo_writel(r, mapbase + LOCOMO_DAC);
940 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
941 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
942
943 if (bit & 1) {
944 r = locomo_readl(mapbase + LOCOMO_DAC);
945 r |= LOCOMO_DAC_SDAOEB;
946 locomo_writel(r, mapbase + LOCOMO_DAC);
947 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
948 } else {
949 r = locomo_readl(mapbase + LOCOMO_DAC);
950 r &= ~(LOCOMO_DAC_SDAOEB);
951 locomo_writel(r, mapbase + LOCOMO_DAC);
952 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
953 }
954
955 udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
956 r = locomo_readl(mapbase + LOCOMO_DAC);
957 r |= LOCOMO_DAC_SCLOEB;
958 locomo_writel(r, mapbase + LOCOMO_DAC);
959 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
960 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
961}
962
963void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
964{
965 struct locomo *lchip = locomo_chip_driver(ldev);
966 int i;
967 unsigned char data;
968 unsigned int r;
969 void *mapbase = lchip->base;
970 unsigned long flags;
971
972 spin_lock_irqsave(&lchip->lock, flags);
973
974 /* Start */
975 udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
976 r = locomo_readl(mapbase + LOCOMO_DAC);
977 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
978 locomo_writel(r, mapbase + LOCOMO_DAC);
979 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
980 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
981 r = locomo_readl(mapbase + LOCOMO_DAC);
982 r &= ~(LOCOMO_DAC_SDAOEB);
983 locomo_writel(r, mapbase + LOCOMO_DAC);
984 udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
985 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
986
987 /* Send slave address and W bit (LSB is W bit) */
988 data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
989 for (i = 1; i <= 8; i++) {
990 locomo_m62332_sendbit(mapbase, data >> (8 - i));
991 }
992
993 /* Check A bit */
994 r = locomo_readl(mapbase + LOCOMO_DAC);
995 r &= ~(LOCOMO_DAC_SCLOEB);
996 locomo_writel(r, mapbase + LOCOMO_DAC);
997 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
998 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
999 r = locomo_readl(mapbase + LOCOMO_DAC);
1000 r &= ~(LOCOMO_DAC_SDAOEB);
1001 locomo_writel(r, mapbase + LOCOMO_DAC);
1002 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1003 r = locomo_readl(mapbase + LOCOMO_DAC);
1004 r |= LOCOMO_DAC_SCLOEB;
1005 locomo_writel(r, mapbase + LOCOMO_DAC);
1006 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1007 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1008 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1009 printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
1010 return;
1011 }
1012
1013 /* Send Sub address (LSB is channel select) */
1014 /* channel = 0 : ch1 select */
1015 /* = 1 : ch2 select */
1016 data = M62332_SUB_ADDR + channel;
1017 for (i = 1; i <= 8; i++) {
1018 locomo_m62332_sendbit(mapbase, data >> (8 - i));
1019 }
1020
1021 /* Check A bit */
1022 r = locomo_readl(mapbase + LOCOMO_DAC);
1023 r &= ~(LOCOMO_DAC_SCLOEB);
1024 locomo_writel(r, mapbase + LOCOMO_DAC);
1025 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1026 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1027 r = locomo_readl(mapbase + LOCOMO_DAC);
1028 r &= ~(LOCOMO_DAC_SDAOEB);
1029 locomo_writel(r, mapbase + LOCOMO_DAC);
1030 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1031 r = locomo_readl(mapbase + LOCOMO_DAC);
1032 r |= LOCOMO_DAC_SCLOEB;
1033 locomo_writel(r, mapbase + LOCOMO_DAC);
1034 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1035 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1036 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1037 printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
1038 return;
1039 }
1040
1041 /* Send DAC data */
1042 for (i = 1; i <= 8; i++) {
1043 locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
1044 }
1045
1046 /* Check A bit */
1047 r = locomo_readl(mapbase + LOCOMO_DAC);
1048 r &= ~(LOCOMO_DAC_SCLOEB);
1049 locomo_writel(r, mapbase + LOCOMO_DAC);
1050 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1051 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1052 r = locomo_readl(mapbase + LOCOMO_DAC);
1053 r &= ~(LOCOMO_DAC_SDAOEB);
1054 locomo_writel(r, mapbase + LOCOMO_DAC);
1055 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1056 r = locomo_readl(mapbase + LOCOMO_DAC);
1057 r |= LOCOMO_DAC_SCLOEB;
1058 locomo_writel(r, mapbase + LOCOMO_DAC);
1059 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1060 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1061 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1062 printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
1063 return;
1064 }
1065
1066 /* stop */
1067 r = locomo_readl(mapbase + LOCOMO_DAC);
1068 r &= ~(LOCOMO_DAC_SCLOEB);
1069 locomo_writel(r, mapbase + LOCOMO_DAC);
1070 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1071 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1072 r = locomo_readl(mapbase + LOCOMO_DAC);
1073 r |= LOCOMO_DAC_SCLOEB;
1074 locomo_writel(r, mapbase + LOCOMO_DAC);
1075 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1076 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1077 r = locomo_readl(mapbase + LOCOMO_DAC);
1078 r |= LOCOMO_DAC_SDAOEB;
1079 locomo_writel(r, mapbase + LOCOMO_DAC);
1080 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1081 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1082
1083 r = locomo_readl(mapbase + LOCOMO_DAC);
1084 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
1085 locomo_writel(r, mapbase + LOCOMO_DAC);
1086 udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
1087 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1088
1089 spin_unlock_irqrestore(&lchip->lock, flags);
1090}
9c5e8fec 1091EXPORT_SYMBOL(locomo_m62332_senddata);
1da177e4 1092
e4423781
RP
1093/*
1094 * Frontlight control
1095 */
1096
e4423781
RP
1097void locomo_frontlight_set(struct locomo_dev *dev, int duty, int vr, int bpwf)
1098{
1099 unsigned long flags;
1100 struct locomo *lchip = locomo_chip_driver(dev);
1101
1102 if (vr)
8d48427e 1103 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 1);
e4423781 1104 else
8d48427e 1105 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
e4423781
RP
1106
1107 spin_lock_irqsave(&lchip->lock, flags);
1108 locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1109 udelay(100);
1110 locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
1111 locomo_writel(bpwf | LOCOMO_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1112 spin_unlock_irqrestore(&lchip->lock, flags);
1113}
9179825c 1114EXPORT_SYMBOL(locomo_frontlight_set);
e4423781 1115
1da177e4
LT
1116/*
1117 * LoCoMo "Register Access Bus."
1118 *
1119 * We model this as a regular bus type, and hang devices directly
1120 * off this.
1121 */
1122static int locomo_match(struct device *_dev, struct device_driver *_drv)
1123{
1124 struct locomo_dev *dev = LOCOMO_DEV(_dev);
1125 struct locomo_driver *drv = LOCOMO_DRV(_drv);
1126
1127 return dev->devid == drv->devid;
1128}
1129
1130static int locomo_bus_suspend(struct device *dev, pm_message_t state)
1131{
1132 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1133 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1134 int ret = 0;
1135
1136 if (drv && drv->suspend)
1137 ret = drv->suspend(ldev, state);
1138 return ret;
1139}
1140
1141static int locomo_bus_resume(struct device *dev)
1142{
1143 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1144 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1145 int ret = 0;
1146
1147 if (drv && drv->resume)
1148 ret = drv->resume(ldev);
1149 return ret;
1150}
1151
1152static int locomo_bus_probe(struct device *dev)
1153{
1154 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1155 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1156 int ret = -ENODEV;
1157
1158 if (drv->probe)
1159 ret = drv->probe(ldev);
1160 return ret;
1161}
1162
1163static int locomo_bus_remove(struct device *dev)
1164{
1165 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1166 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1167 int ret = 0;
1168
1169 if (drv->remove)
1170 ret = drv->remove(ldev);
1171 return ret;
1172}
1173
1174struct bus_type locomo_bus_type = {
1175 .name = "locomo-bus",
1176 .match = locomo_match,
306955be
RK
1177 .probe = locomo_bus_probe,
1178 .remove = locomo_bus_remove,
1da177e4
LT
1179 .suspend = locomo_bus_suspend,
1180 .resume = locomo_bus_resume,
1181};
1182
1183int locomo_driver_register(struct locomo_driver *driver)
1184{
1da177e4
LT
1185 driver->drv.bus = &locomo_bus_type;
1186 return driver_register(&driver->drv);
1187}
9c5e8fec 1188EXPORT_SYMBOL(locomo_driver_register);
1da177e4
LT
1189
1190void locomo_driver_unregister(struct locomo_driver *driver)
1191{
1192 driver_unregister(&driver->drv);
1193}
9c5e8fec 1194EXPORT_SYMBOL(locomo_driver_unregister);
1da177e4
LT
1195
1196static int __init locomo_init(void)
1197{
1198 int ret = bus_register(&locomo_bus_type);
1199 if (ret == 0)
3ae5eaec 1200 platform_driver_register(&locomo_device_driver);
1da177e4
LT
1201 return ret;
1202}
1203
1204static void __exit locomo_exit(void)
1205{
3ae5eaec 1206 platform_driver_unregister(&locomo_device_driver);
1da177e4
LT
1207 bus_unregister(&locomo_bus_type);
1208}
1209
1210module_init(locomo_init);
1211module_exit(locomo_exit);
1212
1213MODULE_DESCRIPTION("Sharp LoCoMo core driver");
1214MODULE_LICENSE("GPL");
1215MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");