Merge commit 'origin/master' into next
[pandora-kernel.git] / arch / powerpc / platforms / 44x / warp.c
1 /*
2  * PIKA Warp(tm) board specific routines
3  *
4  * Copyright (c) 2008-2009 PIKA Technologies
5  *   Sean MacLennan <smaclennan@pikatech.com>
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12 #include <linux/init.h>
13 #include <linux/of_platform.h>
14 #include <linux/kthread.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/of_gpio.h>
19
20 #include <asm/machdep.h>
21 #include <asm/prom.h>
22 #include <asm/udbg.h>
23 #include <asm/time.h>
24 #include <asm/uic.h>
25 #include <asm/ppc4xx.h>
26
27
28 static __initdata struct of_device_id warp_of_bus[] = {
29         { .compatible = "ibm,plb4", },
30         { .compatible = "ibm,opb", },
31         { .compatible = "ibm,ebc", },
32         {},
33 };
34
35 static int __init warp_device_probe(void)
36 {
37         of_platform_bus_probe(NULL, warp_of_bus, NULL);
38         return 0;
39 }
40 machine_device_initcall(warp, warp_device_probe);
41
42 static int __init warp_probe(void)
43 {
44         unsigned long root = of_get_flat_dt_root();
45
46         if (!of_flat_dt_is_compatible(root, "pika,warp"))
47                 return 0;
48
49         /* For __dma_alloc_coherent */
50         ISA_DMA_THRESHOLD = ~0L;
51
52         return 1;
53 }
54
55 define_machine(warp) {
56         .name           = "Warp",
57         .probe          = warp_probe,
58         .progress       = udbg_progress,
59         .init_IRQ       = uic_init_tree,
60         .get_irq        = uic_get_irq,
61         .restart        = ppc4xx_reset_system,
62         .calibrate_decr = generic_calibrate_decr,
63 };
64
65
66 static u32 post_info;
67
68 /* I am not sure this is the best place for this... */
69 static int __init warp_post_info(void)
70 {
71         struct device_node *np;
72         void __iomem *fpga;
73         u32 post1, post2;
74
75         /* Sighhhh... POST information is in the sd area. */
76         np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
77         if (np == NULL)
78                 return -ENOENT;
79
80         fpga = of_iomap(np, 0);
81         of_node_put(np);
82         if (fpga == NULL)
83                 return -ENOENT;
84
85         post1 = in_be32(fpga + 0x40);
86         post2 = in_be32(fpga + 0x44);
87
88         iounmap(fpga);
89
90         if (post1 || post2) {
91                 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
92                 post_info = 1;
93         } else
94                 printk(KERN_INFO "Warp POST OK\n");
95
96         return 0;
97 }
98
99
100 #ifdef CONFIG_SENSORS_AD7414
101
102 static LIST_HEAD(dtm_shutdown_list);
103 static void __iomem *dtm_fpga;
104 static unsigned green_led, red_led;
105
106
107 struct dtm_shutdown {
108         struct list_head list;
109         void (*func)(void *arg);
110         void *arg;
111 };
112
113
114 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
115 {
116         struct dtm_shutdown *shutdown;
117
118         shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
119         if (shutdown == NULL)
120                 return -ENOMEM;
121
122         shutdown->func = func;
123         shutdown->arg = arg;
124
125         list_add(&shutdown->list, &dtm_shutdown_list);
126
127         return 0;
128 }
129
130 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
131 {
132         struct dtm_shutdown *shutdown;
133
134         list_for_each_entry(shutdown, &dtm_shutdown_list, list)
135                 if (shutdown->func == func && shutdown->arg == arg) {
136                         list_del(&shutdown->list);
137                         kfree(shutdown);
138                         return 0;
139                 }
140
141         return -EINVAL;
142 }
143
144 static irqreturn_t temp_isr(int irq, void *context)
145 {
146         struct dtm_shutdown *shutdown;
147         int value = 1;
148
149         local_irq_disable();
150
151         gpio_set_value(green_led, 0);
152
153         /* Run through the shutdown list. */
154         list_for_each_entry(shutdown, &dtm_shutdown_list, list)
155                 shutdown->func(shutdown->arg);
156
157         printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
158
159         while (1) {
160                 if (dtm_fpga) {
161                         unsigned reset = in_be32(dtm_fpga + 0x14);
162                         out_be32(dtm_fpga + 0x14, reset);
163                 }
164
165                 gpio_set_value(red_led, value);
166                 value ^= 1;
167                 mdelay(500);
168         }
169 }
170
171 static int pika_setup_leds(void)
172 {
173         struct device_node *np, *child;
174
175         np = of_find_compatible_node(NULL, NULL, "gpio-leds");
176         if (!np) {
177                 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
178                 return -ENOENT;
179         }
180
181         for_each_child_of_node(np, child)
182                 if (strcmp(child->name, "green") == 0) {
183                         green_led = of_get_gpio(child, 0);
184                         /* Turn back on the green LED */
185                         gpio_set_value(green_led, 1);
186                 } else if (strcmp(child->name, "red") == 0) {
187                         red_led = of_get_gpio(child, 0);
188                         /* Set based on post */
189                         gpio_set_value(red_led, post_info);
190                 }
191
192         of_node_put(np);
193
194         return 0;
195 }
196
197 static void pika_setup_critical_temp(struct i2c_client *client)
198 {
199         struct device_node *np;
200         int irq, rc;
201
202         /* Do this before enabling critical temp interrupt since we
203          * may immediately interrupt.
204          */
205         pika_setup_leds();
206
207         /* These registers are in 1 degree increments. */
208         i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
209         i2c_smbus_write_byte_data(client, 3,  0); /* Tlow */
210
211         np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
212         if (np == NULL) {
213                 printk(KERN_ERR __FILE__ ": Unable to find ad7414\n");
214                 return;
215         }
216
217         irq = irq_of_parse_and_map(np, 0);
218         of_node_put(np);
219         if (irq  == NO_IRQ) {
220                 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
221                 return;
222         }
223
224         rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
225         if (rc) {
226                 printk(KERN_ERR __FILE__
227                        ": Unable to request ad7414 irq %d = %d\n", irq, rc);
228                 return;
229         }
230 }
231
232 static inline void pika_dtm_check_fan(void __iomem *fpga)
233 {
234         static int fan_state;
235         u32 fan = in_be32(fpga + 0x34) & (1 << 14);
236
237         if (fan_state != fan) {
238                 fan_state = fan;
239                 if (fan)
240                         printk(KERN_WARNING "Fan rotation error detected."
241                                    " Please check hardware.\n");
242         }
243 }
244
245 static int pika_dtm_thread(void __iomem *fpga)
246 {
247         struct i2c_adapter *adap;
248         struct i2c_client *client;
249
250         /* We loop in case either driver was compiled as a module and
251          * has not been insmoded yet.
252          */
253         while (!(adap = i2c_get_adapter(0))) {
254                 set_current_state(TASK_INTERRUPTIBLE);
255                 schedule_timeout(HZ);
256         }
257
258         while (1) {
259                 list_for_each_entry(client, &adap->clients, list)
260                         if (client->addr == 0x4a)
261                                 goto found_it;
262
263                 set_current_state(TASK_INTERRUPTIBLE);
264                 schedule_timeout(HZ);
265         }
266
267 found_it:
268         pika_setup_critical_temp(client);
269
270         i2c_put_adapter(adap);
271
272         printk(KERN_INFO "PIKA DTM thread running.\n");
273
274         while (!kthread_should_stop()) {
275                 int val;
276
277                 val = i2c_smbus_read_word_data(client, 0);
278                 if (val < 0)
279                         dev_dbg(&client->dev, "DTM read temp failed.\n");
280                 else {
281                         s16 temp = swab16(val);
282                         out_be32(fpga + 0x20, temp);
283                 }
284
285                 pika_dtm_check_fan(fpga);
286
287                 set_current_state(TASK_INTERRUPTIBLE);
288                 schedule_timeout(HZ);
289         }
290
291         return 0;
292 }
293
294
295 static int __init pika_dtm_start(void)
296 {
297         struct task_struct *dtm_thread;
298         struct device_node *np;
299
300         np = of_find_compatible_node(NULL, NULL, "pika,fpga");
301         if (np == NULL)
302                 return -ENOENT;
303
304         dtm_fpga = of_iomap(np, 0);
305         of_node_put(np);
306         if (dtm_fpga == NULL)
307                 return -ENOENT;
308
309         /* Must get post info before thread starts. */
310         warp_post_info();
311
312         dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
313         if (IS_ERR(dtm_thread)) {
314                 iounmap(dtm_fpga);
315                 return PTR_ERR(dtm_thread);
316         }
317
318         return 0;
319 }
320 machine_late_initcall(warp, pika_dtm_start);
321
322 #else /* !CONFIG_SENSORS_AD7414 */
323
324 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
325 {
326         return 0;
327 }
328
329 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
330 {
331         return 0;
332 }
333
334 machine_late_initcall(warp, warp_post_info);
335
336 #endif
337
338 EXPORT_SYMBOL(pika_dtm_register_shutdown);
339 EXPORT_SYMBOL(pika_dtm_unregister_shutdown);