blob: 283051eaf931c7b1f02b58690ec7239d25eed9d7 [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
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/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;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -070063#ifdef CONFIG_PM
64 void *saved_state;
65#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
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 },
Dirk Opfera2025e72006-09-25 22:41:47 +0100127 {
128 .devid = LOCOMO_DEVID_SPI,
129 .irq = {},
130 .name = "locomo-spi",
131 .offset = LOCOMO_SPI,
132 .length = 0x30,
133 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
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
Russell King10dd5ce2006-11-23 11:41:32 +0000169static void locomo_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000172 struct irq_desc *d;
173 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* Acknowledge the parent IRQ */
176 desc->chip->ack(irq);
177
178 /* check why this interrupt was generated */
179 req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
180
181 if (req) {
182 /* generate the next interrupt(s) */
183 irq = LOCOMO_IRQ_START;
184 d = irq_desc + irq;
185 for (i = 0; i <= 3; i++, d++, irq++) {
186 if (req & (0x0100 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700187 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 }
191 }
192}
193
194static void locomo_ack_irq(unsigned int irq)
195{
196}
197
198static void locomo_mask_irq(unsigned int irq)
199{
Russell King10dd5ce2006-11-23 11:41:32 +0000200 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned int r;
202 r = locomo_readl(mapbase + LOCOMO_ICR);
203 r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
204 locomo_writel(r, mapbase + LOCOMO_ICR);
205}
206
207static void locomo_unmask_irq(unsigned int irq)
208{
Russell King10dd5ce2006-11-23 11:41:32 +0000209 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int r;
211 r = locomo_readl(mapbase + LOCOMO_ICR);
212 r |= (0x0010 << (irq - LOCOMO_IRQ_START));
213 locomo_writel(r, mapbase + LOCOMO_ICR);
214}
215
David Brownell38c677c2006-08-01 22:26:25 +0100216static struct irq_chip locomo_chip = {
217 .name = "LOCOMO",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .ack = locomo_ack_irq,
219 .mask = locomo_mask_irq,
220 .unmask = locomo_unmask_irq,
221};
222
Russell King10dd5ce2006-11-23 11:41:32 +0000223static void locomo_key_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Russell King10dd5ce2006-11-23 11:41:32 +0000225 struct irq_desc *d;
226 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC) & 0x0001) {
229 d = irq_desc + LOCOMO_IRQ_KEY_START;
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700230 desc_handle_irq(LOCOMO_IRQ_KEY_START, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232}
233
234static void locomo_key_ack_irq(unsigned int irq)
235{
Russell King10dd5ce2006-11-23 11:41:32 +0000236 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned int r;
238 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
239 r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
240 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
241}
242
243static void locomo_key_mask_irq(unsigned int irq)
244{
Russell King10dd5ce2006-11-23 11:41:32 +0000245 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned int r;
247 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
248 r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
249 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
250}
251
252static void locomo_key_unmask_irq(unsigned int irq)
253{
Russell King10dd5ce2006-11-23 11:41:32 +0000254 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned int r;
256 r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
257 r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
258 locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
259}
260
David Brownell38c677c2006-08-01 22:26:25 +0100261static struct irq_chip locomo_key_chip = {
262 .name = "LOCOMO-key",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 .ack = locomo_key_ack_irq,
264 .mask = locomo_key_mask_irq,
265 .unmask = locomo_key_unmask_irq,
266};
267
Russell King10dd5ce2006-11-23 11:41:32 +0000268static void locomo_gpio_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000271 struct irq_desc *d;
272 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 req = locomo_readl(mapbase + LOCOMO_GIR) &
275 locomo_readl(mapbase + LOCOMO_GPD) &
276 0xffff;
277
278 if (req) {
279 irq = LOCOMO_IRQ_GPIO_START;
280 d = irq_desc + LOCOMO_IRQ_GPIO_START;
281 for (i = 0; i <= 15; i++, irq++, d++) {
282 if (req & (0x0001 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700283 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 }
286 }
287}
288
289static void locomo_gpio_ack_irq(unsigned int irq)
290{
Russell King10dd5ce2006-11-23 11:41:32 +0000291 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 unsigned int r;
293 r = locomo_readl(mapbase + LOCOMO_GWE);
294 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
295 locomo_writel(r, mapbase + LOCOMO_GWE);
296
297 r = locomo_readl(mapbase + LOCOMO_GIS);
298 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
299 locomo_writel(r, mapbase + LOCOMO_GIS);
300
301 r = locomo_readl(mapbase + LOCOMO_GWE);
302 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
303 locomo_writel(r, mapbase + LOCOMO_GWE);
304}
305
306static void locomo_gpio_mask_irq(unsigned int irq)
307{
Russell King10dd5ce2006-11-23 11:41:32 +0000308 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unsigned int r;
310 r = locomo_readl(mapbase + LOCOMO_GIE);
311 r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
312 locomo_writel(r, mapbase + LOCOMO_GIE);
313}
314
315static void locomo_gpio_unmask_irq(unsigned int irq)
316{
Russell King10dd5ce2006-11-23 11:41:32 +0000317 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned int r;
319 r = locomo_readl(mapbase + LOCOMO_GIE);
320 r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
321 locomo_writel(r, mapbase + LOCOMO_GIE);
322}
323
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100324static int GPIO_IRQ_rising_edge;
325static int GPIO_IRQ_falling_edge;
326
327static int locomo_gpio_type(unsigned int irq, unsigned int type)
328{
329 unsigned int mask;
330 void __iomem *mapbase = get_irq_chip_data(irq);
331
332 mask = 1 << (irq - LOCOMO_IRQ_GPIO_START);
333
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100334 if (type == IRQ_TYPE_PROBE) {
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100335 if ((GPIO_IRQ_rising_edge | GPIO_IRQ_falling_edge) & mask)
336 return 0;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100337 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100338 }
339
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100340 if (type & IRQ_TYPE_EDGE_RISING)
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100341 GPIO_IRQ_rising_edge |= mask;
342 else
343 GPIO_IRQ_rising_edge &= ~mask;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100344 if (type & IRQ_TYPE_EDGE_FALLING)
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100345 GPIO_IRQ_falling_edge |= mask;
346 else
347 GPIO_IRQ_falling_edge &= ~mask;
348 locomo_writel(GPIO_IRQ_rising_edge, mapbase + LOCOMO_GRIE);
349 locomo_writel(GPIO_IRQ_falling_edge, mapbase + LOCOMO_GFIE);
350
351 return 0;
352}
353
David Brownell38c677c2006-08-01 22:26:25 +0100354static struct irq_chip locomo_gpio_chip = {
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100355 .name = "LOCOMO-gpio",
356 .ack = locomo_gpio_ack_irq,
357 .mask = locomo_gpio_mask_irq,
358 .unmask = locomo_gpio_unmask_irq,
359 .set_type = locomo_gpio_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
Russell King10dd5ce2006-11-23 11:41:32 +0000362static void locomo_lt_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Russell King10dd5ce2006-11-23 11:41:32 +0000364 struct irq_desc *d;
365 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
368 d = irq_desc + LOCOMO_IRQ_LT_START;
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700369 desc_handle_irq(LOCOMO_IRQ_LT_START, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371}
372
373static void locomo_lt_ack_irq(unsigned int irq)
374{
Russell King10dd5ce2006-11-23 11:41:32 +0000375 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned int r;
377 r = locomo_readl(mapbase + LOCOMO_LTINT);
378 r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
379 locomo_writel(r, mapbase + LOCOMO_LTINT);
380}
381
382static void locomo_lt_mask_irq(unsigned int irq)
383{
Russell King10dd5ce2006-11-23 11:41:32 +0000384 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned int r;
386 r = locomo_readl(mapbase + LOCOMO_LTINT);
387 r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
388 locomo_writel(r, mapbase + LOCOMO_LTINT);
389}
390
391static void locomo_lt_unmask_irq(unsigned int irq)
392{
Russell King10dd5ce2006-11-23 11:41:32 +0000393 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned int r;
395 r = locomo_readl(mapbase + LOCOMO_LTINT);
396 r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
397 locomo_writel(r, mapbase + LOCOMO_LTINT);
398}
399
David Brownell38c677c2006-08-01 22:26:25 +0100400static struct irq_chip locomo_lt_chip = {
401 .name = "LOCOMO-lt",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .ack = locomo_lt_ack_irq,
403 .mask = locomo_lt_mask_irq,
404 .unmask = locomo_lt_unmask_irq,
405};
406
Russell King10dd5ce2006-11-23 11:41:32 +0000407static void locomo_spi_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000410 struct irq_desc *d;
411 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Dirk Opfera2025e72006-09-25 22:41:47 +0100413 req = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIR) & 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (req) {
415 irq = LOCOMO_IRQ_SPI_START;
416 d = irq_desc + irq;
417
418 for (i = 0; i <= 3; i++, irq++, d++) {
419 if (req & (0x0001 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700420 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 }
423 }
424}
425
426static void locomo_spi_ack_irq(unsigned int irq)
427{
Russell King10dd5ce2006-11-23 11:41:32 +0000428 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100430 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100432 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dirk Opfera2025e72006-09-25 22:41:47 +0100434 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100436 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dirk Opfera2025e72006-09-25 22:41:47 +0100438 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100440 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443static void locomo_spi_mask_irq(unsigned int irq)
444{
Russell King10dd5ce2006-11-23 11:41:32 +0000445 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100447 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100449 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static void locomo_spi_unmask_irq(unsigned int irq)
453{
Russell King10dd5ce2006-11-23 11:41:32 +0000454 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100456 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100458 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
David Brownell38c677c2006-08-01 22:26:25 +0100461static struct irq_chip locomo_spi_chip = {
462 .name = "LOCOMO-spi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 .ack = locomo_spi_ack_irq,
464 .mask = locomo_spi_mask_irq,
465 .unmask = locomo_spi_unmask_irq,
466};
467
468static void locomo_setup_irq(struct locomo *lchip)
469{
470 int irq;
Russell King54815362006-03-15 15:43:04 +0000471 void __iomem *irqbase = lchip->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * Install handler for IRQ_LOCOMO_HW.
475 */
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100476 set_irq_type(lchip->irq, IRQ_TYPE_EDGE_FALLING);
Russell King10dd5ce2006-11-23 11:41:32 +0000477 set_irq_chip_data(lchip->irq, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 set_irq_chained_handler(lchip->irq, locomo_handler);
479
480 /* Install handlers for IRQ_LOCOMO_*_BASE */
481 set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000482 set_irq_chip_data(IRQ_LOCOMO_KEY_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000486 set_irq_chip_data(IRQ_LOCOMO_GPIO_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000490 set_irq_chip_data(IRQ_LOCOMO_LT_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000494 set_irq_chip_data(IRQ_LOCOMO_SPI_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
498 set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000499 set_irq_chip_data(LOCOMO_IRQ_KEY_START, irqbase);
500 set_irq_handler(LOCOMO_IRQ_KEY_START, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
502
503 /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
504 for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
505 set_irq_chip(irq, &locomo_gpio_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000506 set_irq_chip_data(irq, irqbase);
507 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
509 }
510
511 /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
512 set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000513 set_irq_chip_data(LOCOMO_IRQ_LT_START, irqbase);
514 set_irq_handler(LOCOMO_IRQ_LT_START, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
516
517 /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100518 for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 4; irq++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 set_irq_chip(irq, &locomo_spi_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000520 set_irq_chip_data(irq, irqbase);
521 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
523 }
524}
525
526
527static void locomo_dev_release(struct device *_dev)
528{
529 struct locomo_dev *dev = LOCOMO_DEV(_dev);
530
531 kfree(dev);
532}
533
534static int
535locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
536{
537 struct locomo_dev *dev;
538 int ret;
539
Russell Kingd2a02b92006-03-20 19:46:41 +0000540 dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!dev) {
542 ret = -ENOMEM;
543 goto out;
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * If the parent device has a DMA mask associated with it,
548 * propagate it down to the children.
549 */
550 if (lchip->dev->dma_mask) {
551 dev->dma_mask = *lchip->dev->dma_mask;
552 dev->dev.dma_mask = &dev->dma_mask;
553 }
554
Kay Sievers3f978702008-05-30 17:42:11 +0200555 dev_set_name(&dev->dev, "%s", info->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dev->devid = info->devid;
557 dev->dev.parent = lchip->dev;
558 dev->dev.bus = &locomo_bus_type;
559 dev->dev.release = locomo_dev_release;
560 dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
561
562 if (info->offset)
563 dev->mapbase = lchip->base + info->offset;
564 else
565 dev->mapbase = 0;
566 dev->length = info->length;
567
568 memmove(dev->irq, info->irq, sizeof(dev->irq));
569
570 ret = device_register(&dev->dev);
571 if (ret) {
572 out:
573 kfree(dev);
574 }
575 return ret;
576}
577
John Lenzb38d9502005-09-08 14:41:54 +0100578#ifdef CONFIG_PM
579
580struct locomo_save_data {
581 u16 LCM_GPO;
582 u16 LCM_SPICT;
583 u16 LCM_GPE;
584 u16 LCM_ASD;
585 u16 LCM_SPIMD;
586};
587
Russell King3ae5eae2005-11-09 22:32:44 +0000588static int locomo_suspend(struct platform_device *dev, pm_message_t state)
John Lenzb38d9502005-09-08 14:41:54 +0100589{
Russell King3ae5eae2005-11-09 22:32:44 +0000590 struct locomo *lchip = platform_get_drvdata(dev);
John Lenzb38d9502005-09-08 14:41:54 +0100591 struct locomo_save_data *save;
592 unsigned long flags;
593
John Lenzb38d9502005-09-08 14:41:54 +0100594 save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
595 if (!save)
596 return -ENOMEM;
597
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700598 lchip->saved_state = save;
John Lenzb38d9502005-09-08 14:41:54 +0100599
600 spin_lock_irqsave(&lchip->lock, flags);
601
602 save->LCM_GPO = locomo_readl(lchip->base + LOCOMO_GPO); /* GPIO */
603 locomo_writel(0x00, lchip->base + LOCOMO_GPO);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100604 save->LCM_SPICT = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPICT); /* SPI */
John Lenzb38d9502005-09-08 14:41:54 +0100605 locomo_writel(0x40, lchip->base + LOCOMO_SPICT);
606 save->LCM_GPE = locomo_readl(lchip->base + LOCOMO_GPE); /* GPIO */
607 locomo_writel(0x00, lchip->base + LOCOMO_GPE);
608 save->LCM_ASD = locomo_readl(lchip->base + LOCOMO_ASD); /* ADSTART */
609 locomo_writel(0x00, lchip->base + LOCOMO_ASD);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100610 save->LCM_SPIMD = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPIMD); /* SPI */
611 locomo_writel(0x3C14, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
John Lenzb38d9502005-09-08 14:41:54 +0100612
613 locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
614 locomo_writel(0x00, lchip->base + LOCOMO_DAC);
615 locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
616
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100617 if ((locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88))
John Lenzb38d9502005-09-08 14:41:54 +0100618 locomo_writel(0x00, lchip->base + LOCOMO_C32K); /* CLK32 off */
619 else
620 /* 18MHz already enabled, so no wait */
621 locomo_writel(0xc1, lchip->base + LOCOMO_C32K); /* CLK32 on */
622
623 locomo_writel(0x00, lchip->base + LOCOMO_TADC); /* 18MHz clock off*/
624 locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC); /* 22MHz/24MHz clock off */
625 locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS); /* FL */
626
627 spin_unlock_irqrestore(&lchip->lock, flags);
628
629 return 0;
630}
631
Russell King3ae5eae2005-11-09 22:32:44 +0000632static int locomo_resume(struct platform_device *dev)
John Lenzb38d9502005-09-08 14:41:54 +0100633{
Russell King3ae5eae2005-11-09 22:32:44 +0000634 struct locomo *lchip = platform_get_drvdata(dev);
John Lenzb38d9502005-09-08 14:41:54 +0100635 struct locomo_save_data *save;
636 unsigned long r;
637 unsigned long flags;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700638
639 save = lchip->saved_state;
John Lenzb38d9502005-09-08 14:41:54 +0100640 if (!save)
641 return 0;
642
643 spin_lock_irqsave(&lchip->lock, flags);
644
645 locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100646 locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPI + LOCOMO_SPICT);
John Lenzb38d9502005-09-08 14:41:54 +0100647 locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
648 locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100649 locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
John Lenzb38d9502005-09-08 14:41:54 +0100650
651 locomo_writel(0x00, lchip->base + LOCOMO_C32K);
652 locomo_writel(0x90, lchip->base + LOCOMO_TADC);
653
654 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
655 r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
656 r &= 0xFEFF;
657 locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
658 locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
659
660 spin_unlock_irqrestore(&lchip->lock, flags);
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700661
662 lchip->saved_state = NULL;
John Lenzb38d9502005-09-08 14:41:54 +0100663 kfree(save);
664
665 return 0;
666}
667#endif
668
Pavel Machek4ebf2d02006-03-15 16:03:03 +0000669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/**
671 * locomo_probe - probe for a single LoCoMo chip.
672 * @phys_addr: physical address of device.
673 *
674 * Probe for a LoCoMo chip. This must be called
675 * before any other locomo-specific code.
676 *
677 * Returns:
678 * %-ENODEV device not found.
679 * %-EBUSY physical address already marked in-use.
680 * %0 successful.
681 */
682static int
683__locomo_probe(struct device *me, struct resource *mem, int irq)
684{
685 struct locomo *lchip;
686 unsigned long r;
687 int i, ret = -ENODEV;
688
Russell Kingd2a02b92006-03-20 19:46:41 +0000689 lchip = kzalloc(sizeof(struct locomo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!lchip)
691 return -ENOMEM;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_lock_init(&lchip->lock);
694
695 lchip->dev = me;
696 dev_set_drvdata(lchip->dev, lchip);
697
698 lchip->phys = mem->start;
699 lchip->irq = irq;
700
701 /*
702 * Map the whole region. This also maps the
703 * registers for our children.
704 */
705 lchip->base = ioremap(mem->start, PAGE_SIZE);
706 if (!lchip->base) {
707 ret = -ENOMEM;
708 goto out;
709 }
710
711 /* locomo initialize */
712 locomo_writel(0, lchip->base + LOCOMO_ICR);
713 /* KEYBOARD */
714 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
715
716 /* GPIO */
717 locomo_writel(0, lchip->base + LOCOMO_GPO);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100718 locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 , lchip->base + LOCOMO_GPE);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100720 locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 , lchip->base + LOCOMO_GPD);
722 locomo_writel(0, lchip->base + LOCOMO_GIE);
723
Richard Purdiee4423782006-06-26 00:26:27 -0700724 /* Frontlight */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
726 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
Pavel Machek4ebf2d02006-03-15 16:03:03 +0000727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Longtime timer */
729 locomo_writel(0, lchip->base + LOCOMO_LTINT);
730 /* SPI */
731 locomo_writel(0, lchip->base + LOCOMO_SPIIE);
732
733 locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
734 r = locomo_readl(lchip->base + LOCOMO_ASD);
735 r |= 0x8000;
736 locomo_writel(r, lchip->base + LOCOMO_ASD);
737
738 locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
739 r = locomo_readl(lchip->base + LOCOMO_HSD);
740 r |= 0x8000;
741 locomo_writel(r, lchip->base + LOCOMO_HSD);
742
743 locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
744
745 /* XON */
746 locomo_writel(0x80, lchip->base + LOCOMO_TADC);
747 udelay(1000);
748 /* CLK9MEN */
749 r = locomo_readl(lchip->base + LOCOMO_TADC);
750 r |= 0x10;
751 locomo_writel(r, lchip->base + LOCOMO_TADC);
752 udelay(100);
753
754 /* init DAC */
755 r = locomo_readl(lchip->base + LOCOMO_DAC);
756 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
757 locomo_writel(r, lchip->base + LOCOMO_DAC);
758
759 r = locomo_readl(lchip->base + LOCOMO_VER);
760 printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
761
762 /*
763 * The interrupt controller must be initialised before any
764 * other device to ensure that the interrupts are available.
765 */
766 if (lchip->irq != NO_IRQ)
767 locomo_setup_irq(lchip);
768
769 for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
770 locomo_init_one_child(lchip, &locomo_devices[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772
773 out:
774 kfree(lchip);
775 return ret;
776}
777
Pavel Macheke24da5d2005-09-04 11:33:12 +0100778static int locomo_remove_child(struct device *dev, void *data)
779{
780 device_unregister(dev);
781 return 0;
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784static void __locomo_remove(struct locomo *lchip)
785{
Pavel Macheke24da5d2005-09-04 11:33:12 +0100786 device_for_each_child(lchip->dev, NULL, locomo_remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (lchip->irq != NO_IRQ) {
789 set_irq_chained_handler(lchip->irq, NULL);
790 set_irq_data(lchip->irq, NULL);
791 }
792
793 iounmap(lchip->base);
794 kfree(lchip);
795}
796
Russell King3ae5eae2005-11-09 22:32:44 +0000797static int locomo_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct resource *mem;
800 int irq;
801
Russell King3ae5eae2005-11-09 22:32:44 +0000802 mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!mem)
804 return -EINVAL;
Russell King3ae5eae2005-11-09 22:32:44 +0000805 irq = platform_get_irq(dev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000806 if (irq < 0)
807 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Russell King3ae5eae2005-11-09 22:32:44 +0000809 return __locomo_probe(&dev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Russell King3ae5eae2005-11-09 22:32:44 +0000812static int locomo_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Pavel Machekc35bf4a2005-11-12 20:25:25 +0000814 struct locomo *lchip = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (lchip) {
817 __locomo_remove(lchip);
Russell King3ae5eae2005-11-09 22:32:44 +0000818 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 return 0;
822}
823
824/*
825 * Not sure if this should be on the system bus or not yet.
826 * We really want some way to register a system device at
827 * the per-machine level, and then have this driver pick
828 * up the registered devices.
829 */
Russell King3ae5eae2005-11-09 22:32:44 +0000830static struct platform_driver locomo_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .probe = locomo_probe,
832 .remove = locomo_remove,
John Lenzb38d9502005-09-08 14:41:54 +0100833#ifdef CONFIG_PM
834 .suspend = locomo_suspend,
835 .resume = locomo_resume,
836#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000837 .driver = {
838 .name = "locomo",
839 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840};
841
842/*
843 * Get the parent device driver (us) structure
844 * from a child function device
845 */
846static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
847{
848 return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
849}
850
Richard Purdie8d484272006-09-25 20:11:48 +0100851void locomo_gpio_set_dir(struct device *dev, unsigned int bits, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Richard Purdie8d484272006-09-25 20:11:48 +0100853 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 unsigned long flags;
855 unsigned int r;
856
Richard Purdie8d484272006-09-25 20:11:48 +0100857 if (!lchip)
858 return;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_lock_irqsave(&lchip->lock, flags);
861
862 r = locomo_readl(lchip->base + LOCOMO_GPD);
Thomas Kunze2a52efb2008-04-29 17:44:54 +0100863 if (dir)
864 r |= bits;
865 else
866 r &= ~bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 locomo_writel(r, lchip->base + LOCOMO_GPD);
868
869 r = locomo_readl(lchip->base + LOCOMO_GPE);
870 if (dir)
871 r |= bits;
872 else
873 r &= ~bits;
874 locomo_writel(r, lchip->base + LOCOMO_GPE);
875
876 spin_unlock_irqrestore(&lchip->lock, flags);
877}
878
Richard Purdie8d484272006-09-25 20:11:48 +0100879int locomo_gpio_read_level(struct device *dev, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Richard Purdie8d484272006-09-25 20:11:48 +0100881 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 unsigned long flags;
883 unsigned int ret;
884
Richard Purdie8d484272006-09-25 20:11:48 +0100885 if (!lchip)
886 return -ENODEV;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 spin_lock_irqsave(&lchip->lock, flags);
889 ret = locomo_readl(lchip->base + LOCOMO_GPL);
890 spin_unlock_irqrestore(&lchip->lock, flags);
891
892 ret &= bits;
893 return ret;
894}
895
Richard Purdie8d484272006-09-25 20:11:48 +0100896int locomo_gpio_read_output(struct device *dev, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Richard Purdie8d484272006-09-25 20:11:48 +0100898 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long flags;
900 unsigned int ret;
901
Richard Purdie8d484272006-09-25 20:11:48 +0100902 if (!lchip)
903 return -ENODEV;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_lock_irqsave(&lchip->lock, flags);
906 ret = locomo_readl(lchip->base + LOCOMO_GPO);
907 spin_unlock_irqrestore(&lchip->lock, flags);
908
909 ret &= bits;
910 return ret;
911}
912
Richard Purdie8d484272006-09-25 20:11:48 +0100913void locomo_gpio_write(struct device *dev, unsigned int bits, unsigned int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Richard Purdie8d484272006-09-25 20:11:48 +0100915 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned long flags;
917 unsigned int r;
918
Richard Purdie8d484272006-09-25 20:11:48 +0100919 if (!lchip)
920 return;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 spin_lock_irqsave(&lchip->lock, flags);
923
924 r = locomo_readl(lchip->base + LOCOMO_GPO);
925 if (set)
926 r |= bits;
927 else
928 r &= ~bits;
929 locomo_writel(r, lchip->base + LOCOMO_GPO);
930
931 spin_unlock_irqrestore(&lchip->lock, flags);
932}
933
934static void locomo_m62332_sendbit(void *mapbase, int bit)
935{
936 unsigned int r;
937
938 r = locomo_readl(mapbase + LOCOMO_DAC);
939 r &= ~(LOCOMO_DAC_SCLOEB);
940 locomo_writel(r, mapbase + LOCOMO_DAC);
941 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
942 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
943 r = locomo_readl(mapbase + LOCOMO_DAC);
944 r &= ~(LOCOMO_DAC_SCLOEB);
945 locomo_writel(r, mapbase + LOCOMO_DAC);
946 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
947 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
948
949 if (bit & 1) {
950 r = locomo_readl(mapbase + LOCOMO_DAC);
951 r |= LOCOMO_DAC_SDAOEB;
952 locomo_writel(r, mapbase + LOCOMO_DAC);
953 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
954 } else {
955 r = locomo_readl(mapbase + LOCOMO_DAC);
956 r &= ~(LOCOMO_DAC_SDAOEB);
957 locomo_writel(r, mapbase + LOCOMO_DAC);
958 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
959 }
960
961 udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
962 r = locomo_readl(mapbase + LOCOMO_DAC);
963 r |= LOCOMO_DAC_SCLOEB;
964 locomo_writel(r, mapbase + LOCOMO_DAC);
965 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
966 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
967}
968
969void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
970{
971 struct locomo *lchip = locomo_chip_driver(ldev);
972 int i;
973 unsigned char data;
974 unsigned int r;
975 void *mapbase = lchip->base;
976 unsigned long flags;
977
978 spin_lock_irqsave(&lchip->lock, flags);
979
980 /* Start */
981 udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
982 r = locomo_readl(mapbase + LOCOMO_DAC);
983 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
984 locomo_writel(r, mapbase + LOCOMO_DAC);
985 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
986 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
987 r = locomo_readl(mapbase + LOCOMO_DAC);
988 r &= ~(LOCOMO_DAC_SDAOEB);
989 locomo_writel(r, mapbase + LOCOMO_DAC);
990 udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
991 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
992
993 /* Send slave address and W bit (LSB is W bit) */
994 data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
995 for (i = 1; i <= 8; i++) {
996 locomo_m62332_sendbit(mapbase, data >> (8 - i));
997 }
998
999 /* Check A bit */
1000 r = locomo_readl(mapbase + LOCOMO_DAC);
1001 r &= ~(LOCOMO_DAC_SCLOEB);
1002 locomo_writel(r, mapbase + LOCOMO_DAC);
1003 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1004 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1005 r = locomo_readl(mapbase + LOCOMO_DAC);
1006 r &= ~(LOCOMO_DAC_SDAOEB);
1007 locomo_writel(r, mapbase + LOCOMO_DAC);
1008 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1009 r = locomo_readl(mapbase + LOCOMO_DAC);
1010 r |= LOCOMO_DAC_SCLOEB;
1011 locomo_writel(r, mapbase + LOCOMO_DAC);
1012 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1013 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1014 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1015 printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
1016 return;
1017 }
1018
1019 /* Send Sub address (LSB is channel select) */
1020 /* channel = 0 : ch1 select */
1021 /* = 1 : ch2 select */
1022 data = M62332_SUB_ADDR + channel;
1023 for (i = 1; i <= 8; i++) {
1024 locomo_m62332_sendbit(mapbase, data >> (8 - i));
1025 }
1026
1027 /* Check A bit */
1028 r = locomo_readl(mapbase + LOCOMO_DAC);
1029 r &= ~(LOCOMO_DAC_SCLOEB);
1030 locomo_writel(r, mapbase + LOCOMO_DAC);
1031 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1032 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1033 r = locomo_readl(mapbase + LOCOMO_DAC);
1034 r &= ~(LOCOMO_DAC_SDAOEB);
1035 locomo_writel(r, mapbase + LOCOMO_DAC);
1036 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1037 r = locomo_readl(mapbase + LOCOMO_DAC);
1038 r |= LOCOMO_DAC_SCLOEB;
1039 locomo_writel(r, mapbase + LOCOMO_DAC);
1040 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1041 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1042 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1043 printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
1044 return;
1045 }
1046
1047 /* Send DAC data */
1048 for (i = 1; i <= 8; i++) {
1049 locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
1050 }
1051
1052 /* Check A bit */
1053 r = locomo_readl(mapbase + LOCOMO_DAC);
1054 r &= ~(LOCOMO_DAC_SCLOEB);
1055 locomo_writel(r, mapbase + LOCOMO_DAC);
1056 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1057 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1058 r = locomo_readl(mapbase + LOCOMO_DAC);
1059 r &= ~(LOCOMO_DAC_SDAOEB);
1060 locomo_writel(r, mapbase + LOCOMO_DAC);
1061 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1062 r = locomo_readl(mapbase + LOCOMO_DAC);
1063 r |= LOCOMO_DAC_SCLOEB;
1064 locomo_writel(r, mapbase + LOCOMO_DAC);
1065 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1066 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1067 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1068 printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
1069 return;
1070 }
1071
1072 /* stop */
1073 r = locomo_readl(mapbase + LOCOMO_DAC);
1074 r &= ~(LOCOMO_DAC_SCLOEB);
1075 locomo_writel(r, mapbase + LOCOMO_DAC);
1076 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1077 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1078 r = locomo_readl(mapbase + LOCOMO_DAC);
1079 r |= LOCOMO_DAC_SCLOEB;
1080 locomo_writel(r, mapbase + LOCOMO_DAC);
1081 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1082 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1083 r = locomo_readl(mapbase + LOCOMO_DAC);
1084 r |= LOCOMO_DAC_SDAOEB;
1085 locomo_writel(r, mapbase + LOCOMO_DAC);
1086 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1087 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1088
1089 r = locomo_readl(mapbase + LOCOMO_DAC);
1090 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
1091 locomo_writel(r, mapbase + LOCOMO_DAC);
1092 udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
1093 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1094
1095 spin_unlock_irqrestore(&lchip->lock, flags);
1096}
1097
1098/*
Richard Purdiee4423782006-06-26 00:26:27 -07001099 * Frontlight control
1100 */
1101
1102static struct locomo *locomo_chip_driver(struct locomo_dev *ldev);
1103
1104void locomo_frontlight_set(struct locomo_dev *dev, int duty, int vr, int bpwf)
1105{
1106 unsigned long flags;
1107 struct locomo *lchip = locomo_chip_driver(dev);
1108
1109 if (vr)
Richard Purdie8d484272006-09-25 20:11:48 +01001110 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 1);
Richard Purdiee4423782006-06-26 00:26:27 -07001111 else
Richard Purdie8d484272006-09-25 20:11:48 +01001112 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
Richard Purdiee4423782006-06-26 00:26:27 -07001113
1114 spin_lock_irqsave(&lchip->lock, flags);
1115 locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1116 udelay(100);
1117 locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
1118 locomo_writel(bpwf | LOCOMO_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1119 spin_unlock_irqrestore(&lchip->lock, flags);
1120}
1121
1122/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * LoCoMo "Register Access Bus."
1124 *
1125 * We model this as a regular bus type, and hang devices directly
1126 * off this.
1127 */
1128static int locomo_match(struct device *_dev, struct device_driver *_drv)
1129{
1130 struct locomo_dev *dev = LOCOMO_DEV(_dev);
1131 struct locomo_driver *drv = LOCOMO_DRV(_drv);
1132
1133 return dev->devid == drv->devid;
1134}
1135
1136static int locomo_bus_suspend(struct device *dev, pm_message_t state)
1137{
1138 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1139 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1140 int ret = 0;
1141
1142 if (drv && drv->suspend)
1143 ret = drv->suspend(ldev, state);
1144 return ret;
1145}
1146
1147static int locomo_bus_resume(struct device *dev)
1148{
1149 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1150 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1151 int ret = 0;
1152
1153 if (drv && drv->resume)
1154 ret = drv->resume(ldev);
1155 return ret;
1156}
1157
1158static int locomo_bus_probe(struct device *dev)
1159{
1160 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1161 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1162 int ret = -ENODEV;
1163
1164 if (drv->probe)
1165 ret = drv->probe(ldev);
1166 return ret;
1167}
1168
1169static int locomo_bus_remove(struct device *dev)
1170{
1171 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1172 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1173 int ret = 0;
1174
1175 if (drv->remove)
1176 ret = drv->remove(ldev);
1177 return ret;
1178}
1179
1180struct bus_type locomo_bus_type = {
1181 .name = "locomo-bus",
1182 .match = locomo_match,
Russell King306955b2006-01-05 14:33:04 +00001183 .probe = locomo_bus_probe,
1184 .remove = locomo_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 .suspend = locomo_bus_suspend,
1186 .resume = locomo_bus_resume,
1187};
1188
1189int locomo_driver_register(struct locomo_driver *driver)
1190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 driver->drv.bus = &locomo_bus_type;
1192 return driver_register(&driver->drv);
1193}
1194
1195void locomo_driver_unregister(struct locomo_driver *driver)
1196{
1197 driver_unregister(&driver->drv);
1198}
1199
1200static int __init locomo_init(void)
1201{
1202 int ret = bus_register(&locomo_bus_type);
1203 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001204 platform_driver_register(&locomo_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return ret;
1206}
1207
1208static void __exit locomo_exit(void)
1209{
Russell King3ae5eae2005-11-09 22:32:44 +00001210 platform_driver_unregister(&locomo_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 bus_unregister(&locomo_bus_type);
1212}
1213
1214module_init(locomo_init);
1215module_exit(locomo_exit);
1216
1217MODULE_DESCRIPTION("Sharp LoCoMo core driver");
1218MODULE_LICENSE("GPL");
1219MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
1220
1221EXPORT_SYMBOL(locomo_driver_register);
1222EXPORT_SYMBOL(locomo_driver_unregister);
1223EXPORT_SYMBOL(locomo_gpio_set_dir);
1224EXPORT_SYMBOL(locomo_gpio_read_level);
1225EXPORT_SYMBOL(locomo_gpio_read_output);
1226EXPORT_SYMBOL(locomo_gpio_write);
1227EXPORT_SYMBOL(locomo_m62332_senddata);