blob: cfe6f4650bc9fffc836136ecc9ee61d7b5ca0a9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/spinlock.h>
27
28#include <asm/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
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;
Russell King54815362006-03-15 15:43:04 +000062 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65struct locomo_dev_info {
66 unsigned long offset;
67 unsigned long length;
68 unsigned int devid;
69 unsigned int irq[1];
70 const char * name;
71};
72
73/* All the locomo devices. If offset is non-zero, the mapbase for the
74 * locomo_dev will be set to the chip base plus offset. If offset is
75 * zero, then the mapbase for the locomo_dev will be set to zero. An
76 * offset of zero means the device only uses GPIOs or other helper
77 * functions inside this file */
78static struct locomo_dev_info locomo_devices[] = {
79 {
80 .devid = LOCOMO_DEVID_KEYBOARD,
81 .irq = {
82 IRQ_LOCOMO_KEY,
83 },
84 .name = "locomo-keyboard",
85 .offset = LOCOMO_KEYBOARD,
86 .length = 16,
87 },
88 {
89 .devid = LOCOMO_DEVID_FRONTLIGHT,
90 .irq = {},
91 .name = "locomo-frontlight",
92 .offset = LOCOMO_FRONTLIGHT,
93 .length = 8,
94
95 },
96 {
97 .devid = LOCOMO_DEVID_BACKLIGHT,
98 .irq = {},
99 .name = "locomo-backlight",
100 .offset = LOCOMO_BACKLIGHT,
101 .length = 8,
102 },
103 {
104 .devid = LOCOMO_DEVID_AUDIO,
105 .irq = {},
106 .name = "locomo-audio",
107 .offset = LOCOMO_AUDIO,
108 .length = 4,
109 },
110 {
111 .devid = LOCOMO_DEVID_LED,
112 .irq = {},
113 .name = "locomo-led",
114 .offset = LOCOMO_LED,
115 .length = 8,
116 },
117 {
118 .devid = LOCOMO_DEVID_UART,
119 .irq = {},
120 .name = "locomo-uart",
121 .offset = 0,
122 .length = 0,
123 },
Dirk Opfera2025e72006-09-25 22:41:47 +0100124 {
125 .devid = LOCOMO_DEVID_SPI,
126 .irq = {},
127 .name = "locomo-spi",
128 .offset = LOCOMO_SPI,
129 .length = 0x30,
130 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133
134/** LoCoMo interrupt handling stuff.
135 * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
136 * that is, there is only one real hardware interrupt
137 * we determine which interrupt it is by reading some IO memory.
138 * We have two levels of expansion, first in the handler for the
139 * hardware interrupt we generate an interrupt
140 * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
141 *
142 * hardware irq reads LOCOMO_ICR & 0x0f00
143 * IRQ_LOCOMO_KEY_BASE
144 * IRQ_LOCOMO_GPIO_BASE
145 * IRQ_LOCOMO_LT_BASE
146 * IRQ_LOCOMO_SPI_BASE
147 * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
148 * IRQ_LOCOMO_KEY
149 * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
150 * IRQ_LOCOMO_GPIO[0-15]
151 * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
152 * IRQ_LOCOMO_LT
153 * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
154 * IRQ_LOCOMO_SPI_RFR
155 * IRQ_LOCOMO_SPI_RFW
156 * IRQ_LOCOMO_SPI_OVRN
157 * IRQ_LOCOMO_SPI_TEND
158 */
159
160#define LOCOMO_IRQ_START (IRQ_LOCOMO_KEY_BASE)
161#define LOCOMO_IRQ_KEY_START (IRQ_LOCOMO_KEY)
162#define LOCOMO_IRQ_GPIO_START (IRQ_LOCOMO_GPIO0)
163#define LOCOMO_IRQ_LT_START (IRQ_LOCOMO_LT)
164#define LOCOMO_IRQ_SPI_START (IRQ_LOCOMO_SPI_RFR)
165
Russell King10dd5ce2006-11-23 11:41:32 +0000166static void locomo_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000169 struct irq_desc *d;
170 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* Acknowledge the parent IRQ */
173 desc->chip->ack(irq);
174
175 /* check why this interrupt was generated */
176 req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
177
178 if (req) {
179 /* generate the next interrupt(s) */
180 irq = LOCOMO_IRQ_START;
181 d = irq_desc + irq;
182 for (i = 0; i <= 3; i++, d++, irq++) {
183 if (req & (0x0100 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700184 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
187 }
188 }
189}
190
191static void locomo_ack_irq(unsigned int irq)
192{
193}
194
195static void locomo_mask_irq(unsigned int irq)
196{
Russell King10dd5ce2006-11-23 11:41:32 +0000197 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned int r;
199 r = locomo_readl(mapbase + LOCOMO_ICR);
200 r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
201 locomo_writel(r, mapbase + LOCOMO_ICR);
202}
203
204static void locomo_unmask_irq(unsigned int irq)
205{
Russell King10dd5ce2006-11-23 11:41:32 +0000206 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned int r;
208 r = locomo_readl(mapbase + LOCOMO_ICR);
209 r |= (0x0010 << (irq - LOCOMO_IRQ_START));
210 locomo_writel(r, mapbase + LOCOMO_ICR);
211}
212
David Brownell38c677c2006-08-01 22:26:25 +0100213static struct irq_chip locomo_chip = {
214 .name = "LOCOMO",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .ack = locomo_ack_irq,
216 .mask = locomo_mask_irq,
217 .unmask = locomo_unmask_irq,
218};
219
Russell King10dd5ce2006-11-23 11:41:32 +0000220static void locomo_key_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Russell King10dd5ce2006-11-23 11:41:32 +0000222 struct irq_desc *d;
223 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC) & 0x0001) {
226 d = irq_desc + LOCOMO_IRQ_KEY_START;
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700227 desc_handle_irq(LOCOMO_IRQ_KEY_START, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229}
230
231static void locomo_key_ack_irq(unsigned int irq)
232{
Russell King10dd5ce2006-11-23 11:41:32 +0000233 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned int r;
235 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
236 r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
237 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
238}
239
240static void locomo_key_mask_irq(unsigned int irq)
241{
Russell King10dd5ce2006-11-23 11:41:32 +0000242 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int r;
244 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
245 r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
246 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
247}
248
249static void locomo_key_unmask_irq(unsigned int irq)
250{
Russell King10dd5ce2006-11-23 11:41:32 +0000251 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned int r;
253 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
254 r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
255 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
256}
257
David Brownell38c677c2006-08-01 22:26:25 +0100258static struct irq_chip locomo_key_chip = {
259 .name = "LOCOMO-key",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .ack = locomo_key_ack_irq,
261 .mask = locomo_key_mask_irq,
262 .unmask = locomo_key_unmask_irq,
263};
264
Russell King10dd5ce2006-11-23 11:41:32 +0000265static void locomo_gpio_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000268 struct irq_desc *d;
269 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 req = locomo_readl(mapbase + LOCOMO_GIR) &
272 locomo_readl(mapbase + LOCOMO_GPD) &
273 0xffff;
274
275 if (req) {
276 irq = LOCOMO_IRQ_GPIO_START;
277 d = irq_desc + LOCOMO_IRQ_GPIO_START;
278 for (i = 0; i <= 15; i++, irq++, d++) {
279 if (req & (0x0001 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700280 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
283 }
284}
285
286static void locomo_gpio_ack_irq(unsigned int irq)
287{
Russell King10dd5ce2006-11-23 11:41:32 +0000288 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 unsigned int r;
290 r = locomo_readl(mapbase + LOCOMO_GWE);
291 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
292 locomo_writel(r, mapbase + LOCOMO_GWE);
293
294 r = locomo_readl(mapbase + LOCOMO_GIS);
295 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
296 locomo_writel(r, mapbase + LOCOMO_GIS);
297
298 r = locomo_readl(mapbase + LOCOMO_GWE);
299 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
300 locomo_writel(r, mapbase + LOCOMO_GWE);
301}
302
303static void locomo_gpio_mask_irq(unsigned int irq)
304{
Russell King10dd5ce2006-11-23 11:41:32 +0000305 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned int r;
307 r = locomo_readl(mapbase + LOCOMO_GIE);
308 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
309 locomo_writel(r, mapbase + LOCOMO_GIE);
310}
311
312static void locomo_gpio_unmask_irq(unsigned int irq)
313{
Russell King10dd5ce2006-11-23 11:41:32 +0000314 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned int r;
316 r = locomo_readl(mapbase + LOCOMO_GIE);
317 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
318 locomo_writel(r, mapbase + LOCOMO_GIE);
319}
320
David Brownell38c677c2006-08-01 22:26:25 +0100321static struct irq_chip locomo_gpio_chip = {
322 .name = "LOCOMO-gpio",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .ack = locomo_gpio_ack_irq,
324 .mask = locomo_gpio_mask_irq,
325 .unmask = locomo_gpio_unmask_irq,
326};
327
Russell King10dd5ce2006-11-23 11:41:32 +0000328static void locomo_lt_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Russell King10dd5ce2006-11-23 11:41:32 +0000330 struct irq_desc *d;
331 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
334 d = irq_desc + LOCOMO_IRQ_LT_START;
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700335 desc_handle_irq(LOCOMO_IRQ_LT_START, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337}
338
339static void locomo_lt_ack_irq(unsigned int irq)
340{
Russell King10dd5ce2006-11-23 11:41:32 +0000341 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned int r;
343 r = locomo_readl(mapbase + LOCOMO_LTINT);
344 r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
345 locomo_writel(r, mapbase + LOCOMO_LTINT);
346}
347
348static void locomo_lt_mask_irq(unsigned int irq)
349{
Russell King10dd5ce2006-11-23 11:41:32 +0000350 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 unsigned int r;
352 r = locomo_readl(mapbase + LOCOMO_LTINT);
353 r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
354 locomo_writel(r, mapbase + LOCOMO_LTINT);
355}
356
357static void locomo_lt_unmask_irq(unsigned int irq)
358{
Russell King10dd5ce2006-11-23 11:41:32 +0000359 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unsigned int r;
361 r = locomo_readl(mapbase + LOCOMO_LTINT);
362 r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
363 locomo_writel(r, mapbase + LOCOMO_LTINT);
364}
365
David Brownell38c677c2006-08-01 22:26:25 +0100366static struct irq_chip locomo_lt_chip = {
367 .name = "LOCOMO-lt",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 .ack = locomo_lt_ack_irq,
369 .mask = locomo_lt_mask_irq,
370 .unmask = locomo_lt_unmask_irq,
371};
372
Russell King10dd5ce2006-11-23 11:41:32 +0000373static void locomo_spi_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000376 struct irq_desc *d;
377 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dirk Opfera2025e72006-09-25 22:41:47 +0100379 req = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIR) & 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (req) {
381 irq = LOCOMO_IRQ_SPI_START;
382 d = irq_desc + irq;
383
384 for (i = 0; i <= 3; i++, irq++, d++) {
385 if (req & (0x0001 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700386 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
389 }
390}
391
392static void locomo_spi_ack_irq(unsigned int irq)
393{
Russell King10dd5ce2006-11-23 11:41:32 +0000394 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100396 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100398 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Dirk Opfera2025e72006-09-25 22:41:47 +0100400 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100402 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dirk Opfera2025e72006-09-25 22:41:47 +0100404 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100406 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static void locomo_spi_mask_irq(unsigned int irq)
410{
Russell King10dd5ce2006-11-23 11:41:32 +0000411 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100413 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100415 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418static void locomo_spi_unmask_irq(unsigned int irq)
419{
Russell King10dd5ce2006-11-23 11:41:32 +0000420 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100422 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100424 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
David Brownell38c677c2006-08-01 22:26:25 +0100427static struct irq_chip locomo_spi_chip = {
428 .name = "LOCOMO-spi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 .ack = locomo_spi_ack_irq,
430 .mask = locomo_spi_mask_irq,
431 .unmask = locomo_spi_unmask_irq,
432};
433
434static void locomo_setup_irq(struct locomo *lchip)
435{
436 int irq;
Russell King54815362006-03-15 15:43:04 +0000437 void __iomem *irqbase = lchip->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /*
440 * Install handler for IRQ_LOCOMO_HW.
441 */
442 set_irq_type(lchip->irq, IRQT_FALLING);
Russell King10dd5ce2006-11-23 11:41:32 +0000443 set_irq_chip_data(lchip->irq, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 set_irq_chained_handler(lchip->irq, locomo_handler);
445
446 /* Install handlers for IRQ_LOCOMO_*_BASE */
447 set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000448 set_irq_chip_data(IRQ_LOCOMO_KEY_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
450 set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
451
452 set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000453 set_irq_chip_data(IRQ_LOCOMO_GPIO_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
455 set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
456
457 set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000458 set_irq_chip_data(IRQ_LOCOMO_LT_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
460 set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
461
462 set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000463 set_irq_chip_data(IRQ_LOCOMO_SPI_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
465 set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
466
467 /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
468 set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000469 set_irq_chip_data(LOCOMO_IRQ_KEY_START, irqbase);
470 set_irq_handler(LOCOMO_IRQ_KEY_START, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
472
473 /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
474 for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
475 set_irq_chip(irq, &locomo_gpio_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000476 set_irq_chip_data(irq, irqbase);
477 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
479 }
480
481 /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
482 set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000483 set_irq_chip_data(LOCOMO_IRQ_LT_START, irqbase);
484 set_irq_handler(LOCOMO_IRQ_LT_START, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
486
487 /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
488 for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
489 set_irq_chip(irq, &locomo_spi_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000490 set_irq_chip_data(irq, irqbase);
491 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
493 }
494}
495
496
497static void locomo_dev_release(struct device *_dev)
498{
499 struct locomo_dev *dev = LOCOMO_DEV(_dev);
500
501 kfree(dev);
502}
503
504static int
505locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
506{
507 struct locomo_dev *dev;
508 int ret;
509
Russell Kingd2a02b92006-03-20 19:46:41 +0000510 dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!dev) {
512 ret = -ENOMEM;
513 goto out;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Pavel Machek7a2c3022006-07-10 16:29:10 +0100516 strncpy(dev->dev.bus_id, info->name, sizeof(dev->dev.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * If the parent device has a DMA mask associated with it,
519 * propagate it down to the children.
520 */
521 if (lchip->dev->dma_mask) {
522 dev->dma_mask = *lchip->dev->dma_mask;
523 dev->dev.dma_mask = &dev->dma_mask;
524 }
525
526 dev->devid = info->devid;
527 dev->dev.parent = lchip->dev;
528 dev->dev.bus = &locomo_bus_type;
529 dev->dev.release = locomo_dev_release;
530 dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
531
532 if (info->offset)
533 dev->mapbase = lchip->base + info->offset;
534 else
535 dev->mapbase = 0;
536 dev->length = info->length;
537
538 memmove(dev->irq, info->irq, sizeof(dev->irq));
539
540 ret = device_register(&dev->dev);
541 if (ret) {
542 out:
543 kfree(dev);
544 }
545 return ret;
546}
547
John Lenzb38d9502005-09-08 14:41:54 +0100548#ifdef CONFIG_PM
549
550struct locomo_save_data {
551 u16 LCM_GPO;
552 u16 LCM_SPICT;
553 u16 LCM_GPE;
554 u16 LCM_ASD;
555 u16 LCM_SPIMD;
556};
557
Russell King3ae5eae2005-11-09 22:32:44 +0000558static int locomo_suspend(struct platform_device *dev, pm_message_t state)
John Lenzb38d9502005-09-08 14:41:54 +0100559{
Russell King3ae5eae2005-11-09 22:32:44 +0000560 struct locomo *lchip = platform_get_drvdata(dev);
John Lenzb38d9502005-09-08 14:41:54 +0100561 struct locomo_save_data *save;
562 unsigned long flags;
563
John Lenzb38d9502005-09-08 14:41:54 +0100564 save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
565 if (!save)
566 return -ENOMEM;
567
Russell King3ae5eae2005-11-09 22:32:44 +0000568 dev->dev.power.saved_state = (void *) save;
John Lenzb38d9502005-09-08 14:41:54 +0100569
570 spin_lock_irqsave(&lchip->lock, flags);
571
572 save->LCM_GPO = locomo_readl(lchip->base + LOCOMO_GPO); /* GPIO */
573 locomo_writel(0x00, lchip->base + LOCOMO_GPO);
574 save->LCM_SPICT = locomo_readl(lchip->base + LOCOMO_SPICT); /* SPI */
575 locomo_writel(0x40, lchip->base + LOCOMO_SPICT);
576 save->LCM_GPE = locomo_readl(lchip->base + LOCOMO_GPE); /* GPIO */
577 locomo_writel(0x00, lchip->base + LOCOMO_GPE);
578 save->LCM_ASD = locomo_readl(lchip->base + LOCOMO_ASD); /* ADSTART */
579 locomo_writel(0x00, lchip->base + LOCOMO_ASD);
580 save->LCM_SPIMD = locomo_readl(lchip->base + LOCOMO_SPIMD); /* SPI */
581 locomo_writel(0x3C14, lchip->base + LOCOMO_SPIMD);
582
583 locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
584 locomo_writel(0x00, lchip->base + LOCOMO_DAC);
585 locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
586
587 if ( (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88) )
588 locomo_writel(0x00, lchip->base + LOCOMO_C32K); /* CLK32 off */
589 else
590 /* 18MHz already enabled, so no wait */
591 locomo_writel(0xc1, lchip->base + LOCOMO_C32K); /* CLK32 on */
592
593 locomo_writel(0x00, lchip->base + LOCOMO_TADC); /* 18MHz clock off*/
594 locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC); /* 22MHz/24MHz clock off */
595 locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS); /* FL */
596
597 spin_unlock_irqrestore(&lchip->lock, flags);
598
599 return 0;
600}
601
Russell King3ae5eae2005-11-09 22:32:44 +0000602static int locomo_resume(struct platform_device *dev)
John Lenzb38d9502005-09-08 14:41:54 +0100603{
Russell King3ae5eae2005-11-09 22:32:44 +0000604 struct locomo *lchip = platform_get_drvdata(dev);
John Lenzb38d9502005-09-08 14:41:54 +0100605 struct locomo_save_data *save;
606 unsigned long r;
607 unsigned long flags;
608
Russell King3ae5eae2005-11-09 22:32:44 +0000609 save = (struct locomo_save_data *) dev->dev.power.saved_state;
John Lenzb38d9502005-09-08 14:41:54 +0100610 if (!save)
611 return 0;
612
613 spin_lock_irqsave(&lchip->lock, flags);
614
615 locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
616 locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPICT);
617 locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
618 locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
619 locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPIMD);
620
621 locomo_writel(0x00, lchip->base + LOCOMO_C32K);
622 locomo_writel(0x90, lchip->base + LOCOMO_TADC);
623
624 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
625 r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
626 r &= 0xFEFF;
627 locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
628 locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
629
630 spin_unlock_irqrestore(&lchip->lock, flags);
John Lenzb38d9502005-09-08 14:41:54 +0100631 kfree(save);
632
633 return 0;
634}
635#endif
636
Pavel Machek4ebf2d02006-03-15 16:03:03 +0000637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/**
639 * locomo_probe - probe for a single LoCoMo chip.
640 * @phys_addr: physical address of device.
641 *
642 * Probe for a LoCoMo chip. This must be called
643 * before any other locomo-specific code.
644 *
645 * Returns:
646 * %-ENODEV device not found.
647 * %-EBUSY physical address already marked in-use.
648 * %0 successful.
649 */
650static int
651__locomo_probe(struct device *me, struct resource *mem, int irq)
652{
653 struct locomo *lchip;
654 unsigned long r;
655 int i, ret = -ENODEV;
656
Russell Kingd2a02b92006-03-20 19:46:41 +0000657 lchip = kzalloc(sizeof(struct locomo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!lchip)
659 return -ENOMEM;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 spin_lock_init(&lchip->lock);
662
663 lchip->dev = me;
664 dev_set_drvdata(lchip->dev, lchip);
665
666 lchip->phys = mem->start;
667 lchip->irq = irq;
668
669 /*
670 * Map the whole region. This also maps the
671 * registers for our children.
672 */
673 lchip->base = ioremap(mem->start, PAGE_SIZE);
674 if (!lchip->base) {
675 ret = -ENOMEM;
676 goto out;
677 }
678
679 /* locomo initialize */
680 locomo_writel(0, lchip->base + LOCOMO_ICR);
681 /* KEYBOARD */
682 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
683
684 /* GPIO */
685 locomo_writel(0, lchip->base + LOCOMO_GPO);
686 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
687 , lchip->base + LOCOMO_GPE);
688 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
689 , lchip->base + LOCOMO_GPD);
690 locomo_writel(0, lchip->base + LOCOMO_GIE);
691
Richard Purdiee4423782006-06-26 00:26:27 -0700692 /* Frontlight */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
694 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
Pavel Machek4ebf2d02006-03-15 16:03:03 +0000695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* Longtime timer */
697 locomo_writel(0, lchip->base + LOCOMO_LTINT);
698 /* SPI */
699 locomo_writel(0, lchip->base + LOCOMO_SPIIE);
700
701 locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
702 r = locomo_readl(lchip->base + LOCOMO_ASD);
703 r |= 0x8000;
704 locomo_writel(r, lchip->base + LOCOMO_ASD);
705
706 locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
707 r = locomo_readl(lchip->base + LOCOMO_HSD);
708 r |= 0x8000;
709 locomo_writel(r, lchip->base + LOCOMO_HSD);
710
711 locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
712
713 /* XON */
714 locomo_writel(0x80, lchip->base + LOCOMO_TADC);
715 udelay(1000);
716 /* CLK9MEN */
717 r = locomo_readl(lchip->base + LOCOMO_TADC);
718 r |= 0x10;
719 locomo_writel(r, lchip->base + LOCOMO_TADC);
720 udelay(100);
721
722 /* init DAC */
723 r = locomo_readl(lchip->base + LOCOMO_DAC);
724 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
725 locomo_writel(r, lchip->base + LOCOMO_DAC);
726
727 r = locomo_readl(lchip->base + LOCOMO_VER);
728 printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
729
730 /*
731 * The interrupt controller must be initialised before any
732 * other device to ensure that the interrupts are available.
733 */
734 if (lchip->irq != NO_IRQ)
735 locomo_setup_irq(lchip);
736
737 for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
738 locomo_init_one_child(lchip, &locomo_devices[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 0;
740
741 out:
742 kfree(lchip);
743 return ret;
744}
745
Pavel Macheke24da5d2005-09-04 11:33:12 +0100746static int locomo_remove_child(struct device *dev, void *data)
747{
748 device_unregister(dev);
749 return 0;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static void __locomo_remove(struct locomo *lchip)
753{
Pavel Macheke24da5d2005-09-04 11:33:12 +0100754 device_for_each_child(lchip->dev, NULL, locomo_remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (lchip->irq != NO_IRQ) {
757 set_irq_chained_handler(lchip->irq, NULL);
758 set_irq_data(lchip->irq, NULL);
759 }
760
761 iounmap(lchip->base);
762 kfree(lchip);
763}
764
Russell King3ae5eae2005-11-09 22:32:44 +0000765static int locomo_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct resource *mem;
768 int irq;
769
Russell King3ae5eae2005-11-09 22:32:44 +0000770 mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!mem)
772 return -EINVAL;
Russell King3ae5eae2005-11-09 22:32:44 +0000773 irq = platform_get_irq(dev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000774 if (irq < 0)
775 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Russell King3ae5eae2005-11-09 22:32:44 +0000777 return __locomo_probe(&dev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Russell King3ae5eae2005-11-09 22:32:44 +0000780static int locomo_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Pavel Machekc35bf4a2005-11-12 20:25:25 +0000782 struct locomo *lchip = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (lchip) {
785 __locomo_remove(lchip);
Russell King3ae5eae2005-11-09 22:32:44 +0000786 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789 return 0;
790}
791
792/*
793 * Not sure if this should be on the system bus or not yet.
794 * We really want some way to register a system device at
795 * the per-machine level, and then have this driver pick
796 * up the registered devices.
797 */
Russell King3ae5eae2005-11-09 22:32:44 +0000798static struct platform_driver locomo_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .probe = locomo_probe,
800 .remove = locomo_remove,
John Lenzb38d9502005-09-08 14:41:54 +0100801#ifdef CONFIG_PM
802 .suspend = locomo_suspend,
803 .resume = locomo_resume,
804#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000805 .driver = {
806 .name = "locomo",
807 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808};
809
810/*
811 * Get the parent device driver (us) structure
812 * from a child function device
813 */
814static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
815{
816 return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
817}
818
Richard Purdie8d484272006-09-25 20:11:48 +0100819void locomo_gpio_set_dir(struct device *dev, unsigned int bits, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Richard Purdie8d484272006-09-25 20:11:48 +0100821 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 unsigned long flags;
823 unsigned int r;
824
Richard Purdie8d484272006-09-25 20:11:48 +0100825 if (!lchip)
826 return;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 spin_lock_irqsave(&lchip->lock, flags);
829
830 r = locomo_readl(lchip->base + LOCOMO_GPD);
831 r &= ~bits;
832 locomo_writel(r, lchip->base + LOCOMO_GPD);
833
834 r = locomo_readl(lchip->base + LOCOMO_GPE);
835 if (dir)
836 r |= bits;
837 else
838 r &= ~bits;
839 locomo_writel(r, lchip->base + LOCOMO_GPE);
840
841 spin_unlock_irqrestore(&lchip->lock, flags);
842}
843
Richard Purdie8d484272006-09-25 20:11:48 +0100844int locomo_gpio_read_level(struct device *dev, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Richard Purdie8d484272006-09-25 20:11:48 +0100846 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 unsigned long flags;
848 unsigned int ret;
849
Richard Purdie8d484272006-09-25 20:11:48 +0100850 if (!lchip)
851 return -ENODEV;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 spin_lock_irqsave(&lchip->lock, flags);
854 ret = locomo_readl(lchip->base + LOCOMO_GPL);
855 spin_unlock_irqrestore(&lchip->lock, flags);
856
857 ret &= bits;
858 return ret;
859}
860
Richard Purdie8d484272006-09-25 20:11:48 +0100861int locomo_gpio_read_output(struct device *dev, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Richard Purdie8d484272006-09-25 20:11:48 +0100863 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned long flags;
865 unsigned int ret;
866
Richard Purdie8d484272006-09-25 20:11:48 +0100867 if (!lchip)
868 return -ENODEV;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 spin_lock_irqsave(&lchip->lock, flags);
871 ret = locomo_readl(lchip->base + LOCOMO_GPO);
872 spin_unlock_irqrestore(&lchip->lock, flags);
873
874 ret &= bits;
875 return ret;
876}
877
Richard Purdie8d484272006-09-25 20:11:48 +0100878void locomo_gpio_write(struct device *dev, unsigned int bits, unsigned int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Richard Purdie8d484272006-09-25 20:11:48 +0100880 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 unsigned long flags;
882 unsigned int r;
883
Richard Purdie8d484272006-09-25 20:11:48 +0100884 if (!lchip)
885 return;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 spin_lock_irqsave(&lchip->lock, flags);
888
889 r = locomo_readl(lchip->base + LOCOMO_GPO);
890 if (set)
891 r |= bits;
892 else
893 r &= ~bits;
894 locomo_writel(r, lchip->base + LOCOMO_GPO);
895
896 spin_unlock_irqrestore(&lchip->lock, flags);
897}
898
899static void locomo_m62332_sendbit(void *mapbase, int bit)
900{
901 unsigned int r;
902
903 r = locomo_readl(mapbase + LOCOMO_DAC);
904 r &= ~(LOCOMO_DAC_SCLOEB);
905 locomo_writel(r, mapbase + LOCOMO_DAC);
906 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
907 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
908 r = locomo_readl(mapbase + LOCOMO_DAC);
909 r &= ~(LOCOMO_DAC_SCLOEB);
910 locomo_writel(r, mapbase + LOCOMO_DAC);
911 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
912 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
913
914 if (bit & 1) {
915 r = locomo_readl(mapbase + LOCOMO_DAC);
916 r |= LOCOMO_DAC_SDAOEB;
917 locomo_writel(r, mapbase + LOCOMO_DAC);
918 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
919 } else {
920 r = locomo_readl(mapbase + LOCOMO_DAC);
921 r &= ~(LOCOMO_DAC_SDAOEB);
922 locomo_writel(r, mapbase + LOCOMO_DAC);
923 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
924 }
925
926 udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
927 r = locomo_readl(mapbase + LOCOMO_DAC);
928 r |= LOCOMO_DAC_SCLOEB;
929 locomo_writel(r, mapbase + LOCOMO_DAC);
930 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
931 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
932}
933
934void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
935{
936 struct locomo *lchip = locomo_chip_driver(ldev);
937 int i;
938 unsigned char data;
939 unsigned int r;
940 void *mapbase = lchip->base;
941 unsigned long flags;
942
943 spin_lock_irqsave(&lchip->lock, flags);
944
945 /* Start */
946 udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
947 r = locomo_readl(mapbase + LOCOMO_DAC);
948 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
949 locomo_writel(r, mapbase + LOCOMO_DAC);
950 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
951 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
952 r = locomo_readl(mapbase + LOCOMO_DAC);
953 r &= ~(LOCOMO_DAC_SDAOEB);
954 locomo_writel(r, mapbase + LOCOMO_DAC);
955 udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
956 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
957
958 /* Send slave address and W bit (LSB is W bit) */
959 data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
960 for (i = 1; i <= 8; i++) {
961 locomo_m62332_sendbit(mapbase, data >> (8 - i));
962 }
963
964 /* Check A bit */
965 r = locomo_readl(mapbase + LOCOMO_DAC);
966 r &= ~(LOCOMO_DAC_SCLOEB);
967 locomo_writel(r, mapbase + LOCOMO_DAC);
968 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
969 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
970 r = locomo_readl(mapbase + LOCOMO_DAC);
971 r &= ~(LOCOMO_DAC_SDAOEB);
972 locomo_writel(r, mapbase + LOCOMO_DAC);
973 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
974 r = locomo_readl(mapbase + LOCOMO_DAC);
975 r |= LOCOMO_DAC_SCLOEB;
976 locomo_writel(r, mapbase + LOCOMO_DAC);
977 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
978 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
979 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
980 printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
981 return;
982 }
983
984 /* Send Sub address (LSB is channel select) */
985 /* channel = 0 : ch1 select */
986 /* = 1 : ch2 select */
987 data = M62332_SUB_ADDR + channel;
988 for (i = 1; i <= 8; i++) {
989 locomo_m62332_sendbit(mapbase, data >> (8 - i));
990 }
991
992 /* Check A bit */
993 r = locomo_readl(mapbase + LOCOMO_DAC);
994 r &= ~(LOCOMO_DAC_SCLOEB);
995 locomo_writel(r, mapbase + LOCOMO_DAC);
996 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
997 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
998 r = locomo_readl(mapbase + LOCOMO_DAC);
999 r &= ~(LOCOMO_DAC_SDAOEB);
1000 locomo_writel(r, mapbase + LOCOMO_DAC);
1001 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1002 r = locomo_readl(mapbase + LOCOMO_DAC);
1003 r |= LOCOMO_DAC_SCLOEB;
1004 locomo_writel(r, mapbase + LOCOMO_DAC);
1005 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1006 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1007 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1008 printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
1009 return;
1010 }
1011
1012 /* Send DAC data */
1013 for (i = 1; i <= 8; i++) {
1014 locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
1015 }
1016
1017 /* Check A bit */
1018 r = locomo_readl(mapbase + LOCOMO_DAC);
1019 r &= ~(LOCOMO_DAC_SCLOEB);
1020 locomo_writel(r, mapbase + LOCOMO_DAC);
1021 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1022 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1023 r = locomo_readl(mapbase + LOCOMO_DAC);
1024 r &= ~(LOCOMO_DAC_SDAOEB);
1025 locomo_writel(r, mapbase + LOCOMO_DAC);
1026 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1027 r = locomo_readl(mapbase + LOCOMO_DAC);
1028 r |= LOCOMO_DAC_SCLOEB;
1029 locomo_writel(r, mapbase + LOCOMO_DAC);
1030 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1031 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1032 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1033 printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
1034 return;
1035 }
1036
1037 /* stop */
1038 r = locomo_readl(mapbase + LOCOMO_DAC);
1039 r &= ~(LOCOMO_DAC_SCLOEB);
1040 locomo_writel(r, mapbase + LOCOMO_DAC);
1041 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1042 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1043 r = locomo_readl(mapbase + LOCOMO_DAC);
1044 r |= LOCOMO_DAC_SCLOEB;
1045 locomo_writel(r, mapbase + LOCOMO_DAC);
1046 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1047 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1048 r = locomo_readl(mapbase + LOCOMO_DAC);
1049 r |= LOCOMO_DAC_SDAOEB;
1050 locomo_writel(r, mapbase + LOCOMO_DAC);
1051 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1052 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1053
1054 r = locomo_readl(mapbase + LOCOMO_DAC);
1055 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
1056 locomo_writel(r, mapbase + LOCOMO_DAC);
1057 udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
1058 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1059
1060 spin_unlock_irqrestore(&lchip->lock, flags);
1061}
1062
1063/*
Richard Purdiee4423782006-06-26 00:26:27 -07001064 * Frontlight control
1065 */
1066
1067static struct locomo *locomo_chip_driver(struct locomo_dev *ldev);
1068
1069void locomo_frontlight_set(struct locomo_dev *dev, int duty, int vr, int bpwf)
1070{
1071 unsigned long flags;
1072 struct locomo *lchip = locomo_chip_driver(dev);
1073
1074 if (vr)
Richard Purdie8d484272006-09-25 20:11:48 +01001075 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 1);
Richard Purdiee4423782006-06-26 00:26:27 -07001076 else
Richard Purdie8d484272006-09-25 20:11:48 +01001077 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
Richard Purdiee4423782006-06-26 00:26:27 -07001078
1079 spin_lock_irqsave(&lchip->lock, flags);
1080 locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1081 udelay(100);
1082 locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
1083 locomo_writel(bpwf | LOCOMO_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1084 spin_unlock_irqrestore(&lchip->lock, flags);
1085}
1086
1087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * LoCoMo "Register Access Bus."
1089 *
1090 * We model this as a regular bus type, and hang devices directly
1091 * off this.
1092 */
1093static int locomo_match(struct device *_dev, struct device_driver *_drv)
1094{
1095 struct locomo_dev *dev = LOCOMO_DEV(_dev);
1096 struct locomo_driver *drv = LOCOMO_DRV(_drv);
1097
1098 return dev->devid == drv->devid;
1099}
1100
1101static int locomo_bus_suspend(struct device *dev, pm_message_t state)
1102{
1103 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1104 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1105 int ret = 0;
1106
1107 if (drv && drv->suspend)
1108 ret = drv->suspend(ldev, state);
1109 return ret;
1110}
1111
1112static int locomo_bus_resume(struct device *dev)
1113{
1114 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1115 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1116 int ret = 0;
1117
1118 if (drv && drv->resume)
1119 ret = drv->resume(ldev);
1120 return ret;
1121}
1122
1123static int locomo_bus_probe(struct device *dev)
1124{
1125 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1126 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1127 int ret = -ENODEV;
1128
1129 if (drv->probe)
1130 ret = drv->probe(ldev);
1131 return ret;
1132}
1133
1134static int locomo_bus_remove(struct device *dev)
1135{
1136 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1137 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1138 int ret = 0;
1139
1140 if (drv->remove)
1141 ret = drv->remove(ldev);
1142 return ret;
1143}
1144
1145struct bus_type locomo_bus_type = {
1146 .name = "locomo-bus",
1147 .match = locomo_match,
Russell King306955b2006-01-05 14:33:04 +00001148 .probe = locomo_bus_probe,
1149 .remove = locomo_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .suspend = locomo_bus_suspend,
1151 .resume = locomo_bus_resume,
1152};
1153
1154int locomo_driver_register(struct locomo_driver *driver)
1155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 driver->drv.bus = &locomo_bus_type;
1157 return driver_register(&driver->drv);
1158}
1159
1160void locomo_driver_unregister(struct locomo_driver *driver)
1161{
1162 driver_unregister(&driver->drv);
1163}
1164
1165static int __init locomo_init(void)
1166{
1167 int ret = bus_register(&locomo_bus_type);
1168 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001169 platform_driver_register(&locomo_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return ret;
1171}
1172
1173static void __exit locomo_exit(void)
1174{
Russell King3ae5eae2005-11-09 22:32:44 +00001175 platform_driver_unregister(&locomo_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 bus_unregister(&locomo_bus_type);
1177}
1178
1179module_init(locomo_init);
1180module_exit(locomo_exit);
1181
1182MODULE_DESCRIPTION("Sharp LoCoMo core driver");
1183MODULE_LICENSE("GPL");
1184MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
1185
1186EXPORT_SYMBOL(locomo_driver_register);
1187EXPORT_SYMBOL(locomo_driver_unregister);
1188EXPORT_SYMBOL(locomo_gpio_set_dir);
1189EXPORT_SYMBOL(locomo_gpio_read_level);
1190EXPORT_SYMBOL(locomo_gpio_read_output);
1191EXPORT_SYMBOL(locomo_gpio_write);
1192EXPORT_SYMBOL(locomo_m62332_senddata);