blob: ae21755872ed2c8e8d6ad4bdaef3207d1fcb09f9 [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;
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
David Brownell38c677c2006-08-01 22:26:25 +0100324static struct irq_chip locomo_gpio_chip = {
325 .name = "LOCOMO-gpio",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .ack = locomo_gpio_ack_irq,
327 .mask = locomo_gpio_mask_irq,
328 .unmask = locomo_gpio_unmask_irq,
329};
330
Russell King10dd5ce2006-11-23 11:41:32 +0000331static void locomo_lt_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Russell King10dd5ce2006-11-23 11:41:32 +0000333 struct irq_desc *d;
334 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
337 d = irq_desc + LOCOMO_IRQ_LT_START;
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700338 desc_handle_irq(LOCOMO_IRQ_LT_START, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340}
341
342static void locomo_lt_ack_irq(unsigned int irq)
343{
Russell King10dd5ce2006-11-23 11:41:32 +0000344 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned int r;
346 r = locomo_readl(mapbase + LOCOMO_LTINT);
347 r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
348 locomo_writel(r, mapbase + LOCOMO_LTINT);
349}
350
351static void locomo_lt_mask_irq(unsigned int irq)
352{
Russell King10dd5ce2006-11-23 11:41:32 +0000353 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned int r;
355 r = locomo_readl(mapbase + LOCOMO_LTINT);
356 r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
357 locomo_writel(r, mapbase + LOCOMO_LTINT);
358}
359
360static void locomo_lt_unmask_irq(unsigned int irq)
361{
Russell King10dd5ce2006-11-23 11:41:32 +0000362 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned int r;
364 r = locomo_readl(mapbase + LOCOMO_LTINT);
365 r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
366 locomo_writel(r, mapbase + LOCOMO_LTINT);
367}
368
David Brownell38c677c2006-08-01 22:26:25 +0100369static struct irq_chip locomo_lt_chip = {
370 .name = "LOCOMO-lt",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 .ack = locomo_lt_ack_irq,
372 .mask = locomo_lt_mask_irq,
373 .unmask = locomo_lt_unmask_irq,
374};
375
Russell King10dd5ce2006-11-23 11:41:32 +0000376static void locomo_spi_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 int req, i;
Russell King10dd5ce2006-11-23 11:41:32 +0000379 struct irq_desc *d;
380 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Dirk Opfera2025e72006-09-25 22:41:47 +0100382 req = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIR) & 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (req) {
384 irq = LOCOMO_IRQ_SPI_START;
385 d = irq_desc + irq;
386
387 for (i = 0; i <= 3; i++, irq++, d++) {
388 if (req & (0x0001 << i)) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700389 desc_handle_irq(irq, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 }
392 }
393}
394
395static void locomo_spi_ack_irq(unsigned int irq)
396{
Russell King10dd5ce2006-11-23 11:41:32 +0000397 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100399 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100401 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Dirk Opfera2025e72006-09-25 22:41:47 +0100403 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100405 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dirk Opfera2025e72006-09-25 22:41:47 +0100407 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100409 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIWE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412static void locomo_spi_mask_irq(unsigned int irq)
413{
Russell King10dd5ce2006-11-23 11:41:32 +0000414 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100416 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100418 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static void locomo_spi_unmask_irq(unsigned int irq)
422{
Russell King10dd5ce2006-11-23 11:41:32 +0000423 void __iomem *mapbase = get_irq_chip_data(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned int r;
Dirk Opfera2025e72006-09-25 22:41:47 +0100425 r = locomo_readl(mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
Dirk Opfera2025e72006-09-25 22:41:47 +0100427 locomo_writel(r, mapbase + LOCOMO_SPI + LOCOMO_SPIIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
David Brownell38c677c2006-08-01 22:26:25 +0100430static struct irq_chip locomo_spi_chip = {
431 .name = "LOCOMO-spi",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 .ack = locomo_spi_ack_irq,
433 .mask = locomo_spi_mask_irq,
434 .unmask = locomo_spi_unmask_irq,
435};
436
437static void locomo_setup_irq(struct locomo *lchip)
438{
439 int irq;
Russell King54815362006-03-15 15:43:04 +0000440 void __iomem *irqbase = lchip->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /*
443 * Install handler for IRQ_LOCOMO_HW.
444 */
445 set_irq_type(lchip->irq, IRQT_FALLING);
Russell King10dd5ce2006-11-23 11:41:32 +0000446 set_irq_chip_data(lchip->irq, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 set_irq_chained_handler(lchip->irq, locomo_handler);
448
449 /* Install handlers for IRQ_LOCOMO_*_BASE */
450 set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000451 set_irq_chip_data(IRQ_LOCOMO_KEY_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
453 set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
454
455 set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000456 set_irq_chip_data(IRQ_LOCOMO_GPIO_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
458 set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
459
460 set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000461 set_irq_chip_data(IRQ_LOCOMO_LT_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
463 set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
464
465 set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000466 set_irq_chip_data(IRQ_LOCOMO_SPI_BASE, irqbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
468 set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
469
470 /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
471 set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000472 set_irq_chip_data(LOCOMO_IRQ_KEY_START, irqbase);
473 set_irq_handler(LOCOMO_IRQ_KEY_START, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
475
476 /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
477 for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
478 set_irq_chip(irq, &locomo_gpio_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000479 set_irq_chip_data(irq, irqbase);
480 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
482 }
483
484 /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
485 set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000486 set_irq_chip_data(LOCOMO_IRQ_LT_START, irqbase);
487 set_irq_handler(LOCOMO_IRQ_LT_START, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
489
490 /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
491 for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
492 set_irq_chip(irq, &locomo_spi_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000493 set_irq_chip_data(irq, irqbase);
494 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
496 }
497}
498
499
500static void locomo_dev_release(struct device *_dev)
501{
502 struct locomo_dev *dev = LOCOMO_DEV(_dev);
503
504 kfree(dev);
505}
506
507static int
508locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
509{
510 struct locomo_dev *dev;
511 int ret;
512
Russell Kingd2a02b92006-03-20 19:46:41 +0000513 dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!dev) {
515 ret = -ENOMEM;
516 goto out;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Pavel Machek7a2c3022006-07-10 16:29:10 +0100519 strncpy(dev->dev.bus_id, info->name, sizeof(dev->dev.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
521 * If the parent device has a DMA mask associated with it,
522 * propagate it down to the children.
523 */
524 if (lchip->dev->dma_mask) {
525 dev->dma_mask = *lchip->dev->dma_mask;
526 dev->dev.dma_mask = &dev->dma_mask;
527 }
528
529 dev->devid = info->devid;
530 dev->dev.parent = lchip->dev;
531 dev->dev.bus = &locomo_bus_type;
532 dev->dev.release = locomo_dev_release;
533 dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
534
535 if (info->offset)
536 dev->mapbase = lchip->base + info->offset;
537 else
538 dev->mapbase = 0;
539 dev->length = info->length;
540
541 memmove(dev->irq, info->irq, sizeof(dev->irq));
542
543 ret = device_register(&dev->dev);
544 if (ret) {
545 out:
546 kfree(dev);
547 }
548 return ret;
549}
550
John Lenzb38d9502005-09-08 14:41:54 +0100551#ifdef CONFIG_PM
552
553struct locomo_save_data {
554 u16 LCM_GPO;
555 u16 LCM_SPICT;
556 u16 LCM_GPE;
557 u16 LCM_ASD;
558 u16 LCM_SPIMD;
559};
560
Russell King3ae5eae2005-11-09 22:32:44 +0000561static int locomo_suspend(struct platform_device *dev, pm_message_t state)
John Lenzb38d9502005-09-08 14:41:54 +0100562{
Russell King3ae5eae2005-11-09 22:32:44 +0000563 struct locomo *lchip = platform_get_drvdata(dev);
John Lenzb38d9502005-09-08 14:41:54 +0100564 struct locomo_save_data *save;
565 unsigned long flags;
566
John Lenzb38d9502005-09-08 14:41:54 +0100567 save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
568 if (!save)
569 return -ENOMEM;
570
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700571 lchip->saved_state = save;
John Lenzb38d9502005-09-08 14:41:54 +0100572
573 spin_lock_irqsave(&lchip->lock, flags);
574
575 save->LCM_GPO = locomo_readl(lchip->base + LOCOMO_GPO); /* GPIO */
576 locomo_writel(0x00, lchip->base + LOCOMO_GPO);
577 save->LCM_SPICT = locomo_readl(lchip->base + LOCOMO_SPICT); /* SPI */
578 locomo_writel(0x40, lchip->base + LOCOMO_SPICT);
579 save->LCM_GPE = locomo_readl(lchip->base + LOCOMO_GPE); /* GPIO */
580 locomo_writel(0x00, lchip->base + LOCOMO_GPE);
581 save->LCM_ASD = locomo_readl(lchip->base + LOCOMO_ASD); /* ADSTART */
582 locomo_writel(0x00, lchip->base + LOCOMO_ASD);
583 save->LCM_SPIMD = locomo_readl(lchip->base + LOCOMO_SPIMD); /* SPI */
584 locomo_writel(0x3C14, lchip->base + LOCOMO_SPIMD);
585
586 locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
587 locomo_writel(0x00, lchip->base + LOCOMO_DAC);
588 locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
589
590 if ( (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88) )
591 locomo_writel(0x00, lchip->base + LOCOMO_C32K); /* CLK32 off */
592 else
593 /* 18MHz already enabled, so no wait */
594 locomo_writel(0xc1, lchip->base + LOCOMO_C32K); /* CLK32 on */
595
596 locomo_writel(0x00, lchip->base + LOCOMO_TADC); /* 18MHz clock off*/
597 locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC); /* 22MHz/24MHz clock off */
598 locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS); /* FL */
599
600 spin_unlock_irqrestore(&lchip->lock, flags);
601
602 return 0;
603}
604
Russell King3ae5eae2005-11-09 22:32:44 +0000605static int locomo_resume(struct platform_device *dev)
John Lenzb38d9502005-09-08 14:41:54 +0100606{
Russell King3ae5eae2005-11-09 22:32:44 +0000607 struct locomo *lchip = platform_get_drvdata(dev);
John Lenzb38d9502005-09-08 14:41:54 +0100608 struct locomo_save_data *save;
609 unsigned long r;
610 unsigned long flags;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700611
612 save = lchip->saved_state;
John Lenzb38d9502005-09-08 14:41:54 +0100613 if (!save)
614 return 0;
615
616 spin_lock_irqsave(&lchip->lock, flags);
617
618 locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
619 locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPICT);
620 locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
621 locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
622 locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPIMD);
623
624 locomo_writel(0x00, lchip->base + LOCOMO_C32K);
625 locomo_writel(0x90, lchip->base + LOCOMO_TADC);
626
627 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
628 r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
629 r &= 0xFEFF;
630 locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
631 locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
632
633 spin_unlock_irqrestore(&lchip->lock, flags);
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700634
635 lchip->saved_state = NULL;
John Lenzb38d9502005-09-08 14:41:54 +0100636 kfree(save);
637
638 return 0;
639}
640#endif
641
Pavel Machek4ebf2d02006-03-15 16:03:03 +0000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/**
644 * locomo_probe - probe for a single LoCoMo chip.
645 * @phys_addr: physical address of device.
646 *
647 * Probe for a LoCoMo chip. This must be called
648 * before any other locomo-specific code.
649 *
650 * Returns:
651 * %-ENODEV device not found.
652 * %-EBUSY physical address already marked in-use.
653 * %0 successful.
654 */
655static int
656__locomo_probe(struct device *me, struct resource *mem, int irq)
657{
658 struct locomo *lchip;
659 unsigned long r;
660 int i, ret = -ENODEV;
661
Russell Kingd2a02b92006-03-20 19:46:41 +0000662 lchip = kzalloc(sizeof(struct locomo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (!lchip)
664 return -ENOMEM;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_lock_init(&lchip->lock);
667
668 lchip->dev = me;
669 dev_set_drvdata(lchip->dev, lchip);
670
671 lchip->phys = mem->start;
672 lchip->irq = irq;
673
674 /*
675 * Map the whole region. This also maps the
676 * registers for our children.
677 */
678 lchip->base = ioremap(mem->start, PAGE_SIZE);
679 if (!lchip->base) {
680 ret = -ENOMEM;
681 goto out;
682 }
683
684 /* locomo initialize */
685 locomo_writel(0, lchip->base + LOCOMO_ICR);
686 /* KEYBOARD */
687 locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
688
689 /* GPIO */
690 locomo_writel(0, lchip->base + LOCOMO_GPO);
691 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
692 , lchip->base + LOCOMO_GPE);
693 locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
694 , lchip->base + LOCOMO_GPD);
695 locomo_writel(0, lchip->base + LOCOMO_GIE);
696
Richard Purdiee4423782006-06-26 00:26:27 -0700697 /* Frontlight */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
699 locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
Pavel Machek4ebf2d02006-03-15 16:03:03 +0000700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Longtime timer */
702 locomo_writel(0, lchip->base + LOCOMO_LTINT);
703 /* SPI */
704 locomo_writel(0, lchip->base + LOCOMO_SPIIE);
705
706 locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
707 r = locomo_readl(lchip->base + LOCOMO_ASD);
708 r |= 0x8000;
709 locomo_writel(r, lchip->base + LOCOMO_ASD);
710
711 locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
712 r = locomo_readl(lchip->base + LOCOMO_HSD);
713 r |= 0x8000;
714 locomo_writel(r, lchip->base + LOCOMO_HSD);
715
716 locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
717
718 /* XON */
719 locomo_writel(0x80, lchip->base + LOCOMO_TADC);
720 udelay(1000);
721 /* CLK9MEN */
722 r = locomo_readl(lchip->base + LOCOMO_TADC);
723 r |= 0x10;
724 locomo_writel(r, lchip->base + LOCOMO_TADC);
725 udelay(100);
726
727 /* init DAC */
728 r = locomo_readl(lchip->base + LOCOMO_DAC);
729 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
730 locomo_writel(r, lchip->base + LOCOMO_DAC);
731
732 r = locomo_readl(lchip->base + LOCOMO_VER);
733 printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
734
735 /*
736 * The interrupt controller must be initialised before any
737 * other device to ensure that the interrupts are available.
738 */
739 if (lchip->irq != NO_IRQ)
740 locomo_setup_irq(lchip);
741
742 for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
743 locomo_init_one_child(lchip, &locomo_devices[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745
746 out:
747 kfree(lchip);
748 return ret;
749}
750
Pavel Macheke24da5d2005-09-04 11:33:12 +0100751static int locomo_remove_child(struct device *dev, void *data)
752{
753 device_unregister(dev);
754 return 0;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static void __locomo_remove(struct locomo *lchip)
758{
Pavel Macheke24da5d2005-09-04 11:33:12 +0100759 device_for_each_child(lchip->dev, NULL, locomo_remove_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (lchip->irq != NO_IRQ) {
762 set_irq_chained_handler(lchip->irq, NULL);
763 set_irq_data(lchip->irq, NULL);
764 }
765
766 iounmap(lchip->base);
767 kfree(lchip);
768}
769
Russell King3ae5eae2005-11-09 22:32:44 +0000770static int locomo_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct resource *mem;
773 int irq;
774
Russell King3ae5eae2005-11-09 22:32:44 +0000775 mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!mem)
777 return -EINVAL;
Russell King3ae5eae2005-11-09 22:32:44 +0000778 irq = platform_get_irq(dev, 0);
David Vrabel48944732006-01-19 17:56:29 +0000779 if (irq < 0)
780 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Russell King3ae5eae2005-11-09 22:32:44 +0000782 return __locomo_probe(&dev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Russell King3ae5eae2005-11-09 22:32:44 +0000785static int locomo_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Pavel Machekc35bf4a2005-11-12 20:25:25 +0000787 struct locomo *lchip = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (lchip) {
790 __locomo_remove(lchip);
Russell King3ae5eae2005-11-09 22:32:44 +0000791 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 return 0;
795}
796
797/*
798 * Not sure if this should be on the system bus or not yet.
799 * We really want some way to register a system device at
800 * the per-machine level, and then have this driver pick
801 * up the registered devices.
802 */
Russell King3ae5eae2005-11-09 22:32:44 +0000803static struct platform_driver locomo_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 .probe = locomo_probe,
805 .remove = locomo_remove,
John Lenzb38d9502005-09-08 14:41:54 +0100806#ifdef CONFIG_PM
807 .suspend = locomo_suspend,
808 .resume = locomo_resume,
809#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000810 .driver = {
811 .name = "locomo",
812 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813};
814
815/*
816 * Get the parent device driver (us) structure
817 * from a child function device
818 */
819static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
820{
821 return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
822}
823
Richard Purdie8d484272006-09-25 20:11:48 +0100824void locomo_gpio_set_dir(struct device *dev, unsigned int bits, unsigned int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Richard Purdie8d484272006-09-25 20:11:48 +0100826 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 unsigned long flags;
828 unsigned int r;
829
Richard Purdie8d484272006-09-25 20:11:48 +0100830 if (!lchip)
831 return;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 spin_lock_irqsave(&lchip->lock, flags);
834
835 r = locomo_readl(lchip->base + LOCOMO_GPD);
836 r &= ~bits;
837 locomo_writel(r, lchip->base + LOCOMO_GPD);
838
839 r = locomo_readl(lchip->base + LOCOMO_GPE);
840 if (dir)
841 r |= bits;
842 else
843 r &= ~bits;
844 locomo_writel(r, lchip->base + LOCOMO_GPE);
845
846 spin_unlock_irqrestore(&lchip->lock, flags);
847}
848
Richard Purdie8d484272006-09-25 20:11:48 +0100849int locomo_gpio_read_level(struct device *dev, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Richard Purdie8d484272006-09-25 20:11:48 +0100851 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned long flags;
853 unsigned int ret;
854
Richard Purdie8d484272006-09-25 20:11:48 +0100855 if (!lchip)
856 return -ENODEV;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_lock_irqsave(&lchip->lock, flags);
859 ret = locomo_readl(lchip->base + LOCOMO_GPL);
860 spin_unlock_irqrestore(&lchip->lock, flags);
861
862 ret &= bits;
863 return ret;
864}
865
Richard Purdie8d484272006-09-25 20:11:48 +0100866int locomo_gpio_read_output(struct device *dev, unsigned int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Richard Purdie8d484272006-09-25 20:11:48 +0100868 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned long flags;
870 unsigned int ret;
871
Richard Purdie8d484272006-09-25 20:11:48 +0100872 if (!lchip)
873 return -ENODEV;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 spin_lock_irqsave(&lchip->lock, flags);
876 ret = locomo_readl(lchip->base + LOCOMO_GPO);
877 spin_unlock_irqrestore(&lchip->lock, flags);
878
879 ret &= bits;
880 return ret;
881}
882
Richard Purdie8d484272006-09-25 20:11:48 +0100883void locomo_gpio_write(struct device *dev, unsigned int bits, unsigned int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Richard Purdie8d484272006-09-25 20:11:48 +0100885 struct locomo *lchip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned long flags;
887 unsigned int r;
888
Richard Purdie8d484272006-09-25 20:11:48 +0100889 if (!lchip)
890 return;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 spin_lock_irqsave(&lchip->lock, flags);
893
894 r = locomo_readl(lchip->base + LOCOMO_GPO);
895 if (set)
896 r |= bits;
897 else
898 r &= ~bits;
899 locomo_writel(r, lchip->base + LOCOMO_GPO);
900
901 spin_unlock_irqrestore(&lchip->lock, flags);
902}
903
904static void locomo_m62332_sendbit(void *mapbase, int bit)
905{
906 unsigned int r;
907
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_DATA_HOLD_TIME); /* 300 nsec */
913 r = locomo_readl(mapbase + LOCOMO_DAC);
914 r &= ~(LOCOMO_DAC_SCLOEB);
915 locomo_writel(r, mapbase + LOCOMO_DAC);
916 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
917 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
918
919 if (bit & 1) {
920 r = locomo_readl(mapbase + LOCOMO_DAC);
921 r |= LOCOMO_DAC_SDAOEB;
922 locomo_writel(r, mapbase + LOCOMO_DAC);
923 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
924 } else {
925 r = locomo_readl(mapbase + LOCOMO_DAC);
926 r &= ~(LOCOMO_DAC_SDAOEB);
927 locomo_writel(r, mapbase + LOCOMO_DAC);
928 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
929 }
930
931 udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
932 r = locomo_readl(mapbase + LOCOMO_DAC);
933 r |= LOCOMO_DAC_SCLOEB;
934 locomo_writel(r, mapbase + LOCOMO_DAC);
935 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
936 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
937}
938
939void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
940{
941 struct locomo *lchip = locomo_chip_driver(ldev);
942 int i;
943 unsigned char data;
944 unsigned int r;
945 void *mapbase = lchip->base;
946 unsigned long flags;
947
948 spin_lock_irqsave(&lchip->lock, flags);
949
950 /* Start */
951 udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
952 r = locomo_readl(mapbase + LOCOMO_DAC);
953 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
954 locomo_writel(r, mapbase + LOCOMO_DAC);
955 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
956 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
957 r = locomo_readl(mapbase + LOCOMO_DAC);
958 r &= ~(LOCOMO_DAC_SDAOEB);
959 locomo_writel(r, mapbase + LOCOMO_DAC);
960 udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
961 udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
962
963 /* Send slave address and W bit (LSB is W bit) */
964 data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
965 for (i = 1; i <= 8; i++) {
966 locomo_m62332_sendbit(mapbase, data >> (8 - i));
967 }
968
969 /* Check A bit */
970 r = locomo_readl(mapbase + LOCOMO_DAC);
971 r &= ~(LOCOMO_DAC_SCLOEB);
972 locomo_writel(r, mapbase + LOCOMO_DAC);
973 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
974 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
975 r = locomo_readl(mapbase + LOCOMO_DAC);
976 r &= ~(LOCOMO_DAC_SDAOEB);
977 locomo_writel(r, mapbase + LOCOMO_DAC);
978 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
979 r = locomo_readl(mapbase + LOCOMO_DAC);
980 r |= LOCOMO_DAC_SCLOEB;
981 locomo_writel(r, mapbase + LOCOMO_DAC);
982 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
983 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
984 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
985 printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
986 return;
987 }
988
989 /* Send Sub address (LSB is channel select) */
990 /* channel = 0 : ch1 select */
991 /* = 1 : ch2 select */
992 data = M62332_SUB_ADDR + channel;
993 for (i = 1; i <= 8; i++) {
994 locomo_m62332_sendbit(mapbase, data >> (8 - i));
995 }
996
997 /* Check A bit */
998 r = locomo_readl(mapbase + LOCOMO_DAC);
999 r &= ~(LOCOMO_DAC_SCLOEB);
1000 locomo_writel(r, mapbase + LOCOMO_DAC);
1001 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1002 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1003 r = locomo_readl(mapbase + LOCOMO_DAC);
1004 r &= ~(LOCOMO_DAC_SDAOEB);
1005 locomo_writel(r, mapbase + LOCOMO_DAC);
1006 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1007 r = locomo_readl(mapbase + LOCOMO_DAC);
1008 r |= LOCOMO_DAC_SCLOEB;
1009 locomo_writel(r, mapbase + LOCOMO_DAC);
1010 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1011 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1012 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1013 printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
1014 return;
1015 }
1016
1017 /* Send DAC data */
1018 for (i = 1; i <= 8; i++) {
1019 locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
1020 }
1021
1022 /* Check A bit */
1023 r = locomo_readl(mapbase + LOCOMO_DAC);
1024 r &= ~(LOCOMO_DAC_SCLOEB);
1025 locomo_writel(r, mapbase + LOCOMO_DAC);
1026 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1027 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1028 r = locomo_readl(mapbase + LOCOMO_DAC);
1029 r &= ~(LOCOMO_DAC_SDAOEB);
1030 locomo_writel(r, mapbase + LOCOMO_DAC);
1031 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1032 r = locomo_readl(mapbase + LOCOMO_DAC);
1033 r |= LOCOMO_DAC_SCLOEB;
1034 locomo_writel(r, mapbase + LOCOMO_DAC);
1035 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1036 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
1037 if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
1038 printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
1039 return;
1040 }
1041
1042 /* stop */
1043 r = locomo_readl(mapbase + LOCOMO_DAC);
1044 r &= ~(LOCOMO_DAC_SCLOEB);
1045 locomo_writel(r, mapbase + LOCOMO_DAC);
1046 udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
1047 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1048 r = locomo_readl(mapbase + LOCOMO_DAC);
1049 r |= LOCOMO_DAC_SCLOEB;
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 r = locomo_readl(mapbase + LOCOMO_DAC);
1054 r |= LOCOMO_DAC_SDAOEB;
1055 locomo_writel(r, mapbase + LOCOMO_DAC);
1056 udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
1057 udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
1058
1059 r = locomo_readl(mapbase + LOCOMO_DAC);
1060 r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
1061 locomo_writel(r, mapbase + LOCOMO_DAC);
1062 udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
1063 udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
1064
1065 spin_unlock_irqrestore(&lchip->lock, flags);
1066}
1067
1068/*
Richard Purdiee4423782006-06-26 00:26:27 -07001069 * Frontlight control
1070 */
1071
1072static struct locomo *locomo_chip_driver(struct locomo_dev *ldev);
1073
1074void locomo_frontlight_set(struct locomo_dev *dev, int duty, int vr, int bpwf)
1075{
1076 unsigned long flags;
1077 struct locomo *lchip = locomo_chip_driver(dev);
1078
1079 if (vr)
Richard Purdie8d484272006-09-25 20:11:48 +01001080 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 1);
Richard Purdiee4423782006-06-26 00:26:27 -07001081 else
Richard Purdie8d484272006-09-25 20:11:48 +01001082 locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
Richard Purdiee4423782006-06-26 00:26:27 -07001083
1084 spin_lock_irqsave(&lchip->lock, flags);
1085 locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1086 udelay(100);
1087 locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
1088 locomo_writel(bpwf | LOCOMO_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
1089 spin_unlock_irqrestore(&lchip->lock, flags);
1090}
1091
1092/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * LoCoMo "Register Access Bus."
1094 *
1095 * We model this as a regular bus type, and hang devices directly
1096 * off this.
1097 */
1098static int locomo_match(struct device *_dev, struct device_driver *_drv)
1099{
1100 struct locomo_dev *dev = LOCOMO_DEV(_dev);
1101 struct locomo_driver *drv = LOCOMO_DRV(_drv);
1102
1103 return dev->devid == drv->devid;
1104}
1105
1106static int locomo_bus_suspend(struct device *dev, pm_message_t state)
1107{
1108 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1109 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1110 int ret = 0;
1111
1112 if (drv && drv->suspend)
1113 ret = drv->suspend(ldev, state);
1114 return ret;
1115}
1116
1117static int locomo_bus_resume(struct device *dev)
1118{
1119 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1120 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1121 int ret = 0;
1122
1123 if (drv && drv->resume)
1124 ret = drv->resume(ldev);
1125 return ret;
1126}
1127
1128static int locomo_bus_probe(struct device *dev)
1129{
1130 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1131 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1132 int ret = -ENODEV;
1133
1134 if (drv->probe)
1135 ret = drv->probe(ldev);
1136 return ret;
1137}
1138
1139static int locomo_bus_remove(struct device *dev)
1140{
1141 struct locomo_dev *ldev = LOCOMO_DEV(dev);
1142 struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
1143 int ret = 0;
1144
1145 if (drv->remove)
1146 ret = drv->remove(ldev);
1147 return ret;
1148}
1149
1150struct bus_type locomo_bus_type = {
1151 .name = "locomo-bus",
1152 .match = locomo_match,
Russell King306955b2006-01-05 14:33:04 +00001153 .probe = locomo_bus_probe,
1154 .remove = locomo_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 .suspend = locomo_bus_suspend,
1156 .resume = locomo_bus_resume,
1157};
1158
1159int locomo_driver_register(struct locomo_driver *driver)
1160{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 driver->drv.bus = &locomo_bus_type;
1162 return driver_register(&driver->drv);
1163}
1164
1165void locomo_driver_unregister(struct locomo_driver *driver)
1166{
1167 driver_unregister(&driver->drv);
1168}
1169
1170static int __init locomo_init(void)
1171{
1172 int ret = bus_register(&locomo_bus_type);
1173 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001174 platform_driver_register(&locomo_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return ret;
1176}
1177
1178static void __exit locomo_exit(void)
1179{
Russell King3ae5eae2005-11-09 22:32:44 +00001180 platform_driver_unregister(&locomo_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 bus_unregister(&locomo_bus_type);
1182}
1183
1184module_init(locomo_init);
1185module_exit(locomo_exit);
1186
1187MODULE_DESCRIPTION("Sharp LoCoMo core driver");
1188MODULE_LICENSE("GPL");
1189MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
1190
1191EXPORT_SYMBOL(locomo_driver_register);
1192EXPORT_SYMBOL(locomo_driver_unregister);
1193EXPORT_SYMBOL(locomo_gpio_set_dir);
1194EXPORT_SYMBOL(locomo_gpio_read_level);
1195EXPORT_SYMBOL(locomo_gpio_read_output);
1196EXPORT_SYMBOL(locomo_gpio_write);
1197EXPORT_SYMBOL(locomo_m62332_senddata);