9916b391971e1f2df7b4b50f4051c90fd46e5b62
[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 #include <linux/of_i2c.h>
20
21 #include <asm/machdep.h>
22 #include <asm/prom.h>
23 #include <asm/udbg.h>
24 #include <asm/time.h>
25 #include <asm/uic.h>
26 #include <asm/ppc4xx.h>
27
28
29 static __initdata struct of_device_id warp_of_bus[] = {
30         { .compatible = "ibm,plb4", },
31         { .compatible = "ibm,opb", },
32         { .compatible = "ibm,ebc", },
33         {},
34 };
35
36 static int __init warp_device_probe(void)
37 {
38         of_platform_bus_probe(NULL, warp_of_bus, NULL);
39         return 0;
40 }
41 machine_device_initcall(warp, warp_device_probe);
42
43 static int __init warp_probe(void)
44 {
45         unsigned long root = of_get_flat_dt_root();
46
47         if (!of_flat_dt_is_compatible(root, "pika,warp"))
48                 return 0;
49
50         /* For __dma_alloc_coherent */
51         ISA_DMA_THRESHOLD = ~0L;
52
53         return 1;
54 }
55
56 define_machine(warp) {
57         .name           = "Warp",
58         .probe          = warp_probe,
59         .progress       = udbg_progress,
60         .init_IRQ       = uic_init_tree,
61         .get_irq        = uic_get_irq,
62         .restart        = ppc4xx_reset_system,
63         .calibrate_decr = generic_calibrate_decr,
64 };
65
66
67 static int __init warp_post_info(void)
68 {
69         struct device_node *np;
70         void __iomem *fpga;
71         u32 post1, post2;
72
73         /* Sighhhh... POST information is in the sd area. */
74         np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
75         if (np == NULL)
76                 return -ENOENT;
77
78         fpga = of_iomap(np, 0);
79         of_node_put(np);
80         if (fpga == NULL)
81                 return -ENOENT;
82
83         post1 = in_be32(fpga + 0x40);
84         post2 = in_be32(fpga + 0x44);
85
86         iounmap(fpga);
87
88         if (post1 || post2)
89                 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
90         else
91                 printk(KERN_INFO "Warp POST OK\n");
92
93         return 0;
94 }
95
96
97 #ifdef CONFIG_SENSORS_AD7414
98
99 static LIST_HEAD(dtm_shutdown_list);
100 static void __iomem *dtm_fpga;
101 static unsigned green_led, red_led;
102
103
104 struct dtm_shutdown {
105         struct list_head list;
106         void (*func)(void *arg);
107         void *arg;
108 };
109
110
111 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
112 {
113         struct dtm_shutdown *shutdown;
114
115         shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
116         if (shutdown == NULL)
117                 return -ENOMEM;
118
119         shutdown->func = func;
120         shutdown->arg = arg;
121
122         list_add(&shutdown->list, &dtm_shutdown_list);
123
124         return 0;
125 }
126
127 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
128 {
129         struct dtm_shutdown *shutdown;
130
131         list_for_each_entry(shutdown, &dtm_shutdown_list, list)
132                 if (shutdown->func == func && shutdown->arg == arg) {
133                         list_del(&shutdown->list);
134                         kfree(shutdown);
135                         return 0;
136                 }
137
138         return -EINVAL;
139 }
140
141 static irqreturn_t temp_isr(int irq, void *context)
142 {
143         struct dtm_shutdown *shutdown;
144         int value = 1;
145
146         local_irq_disable();
147
148         gpio_set_value(green_led, 0);
149
150         /* Run through the shutdown list. */
151         list_for_each_entry(shutdown, &dtm_shutdown_list, list)
152                 shutdown->func(shutdown->arg);
153
154         printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
155
156         while (1) {
157                 if (dtm_fpga) {
158                         unsigned reset = in_be32(dtm_fpga + 0x14);
159                         out_be32(dtm_fpga + 0x14, reset);
160                 }
161
162                 gpio_set_value(red_led, value);
163                 value ^= 1;
164                 mdelay(500);
165         }
166 }
167
168 static int pika_setup_leds(void)
169 {
170         struct device_node *np, *child;
171
172         np = of_find_compatible_node(NULL, NULL, "gpio-leds");
173         if (!np) {
174                 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
175                 return -ENOENT;
176         }
177
178         for_each_child_of_node(np, child)
179                 if (strcmp(child->name, "green") == 0)
180                         green_led = of_get_gpio(child, 0);
181                 else if (strcmp(child->name, "red") == 0)
182                         red_led = of_get_gpio(child, 0);
183
184         of_node_put(np);
185
186         return 0;
187 }
188
189 static void pika_setup_critical_temp(struct device_node *np,
190                                      struct i2c_client *client)
191 {
192         int irq, rc;
193
194         /* Do this before enabling critical temp interrupt since we
195          * may immediately interrupt.
196          */
197         pika_setup_leds();
198
199         /* These registers are in 1 degree increments. */
200         i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
201         i2c_smbus_write_byte_data(client, 3,  0); /* Tlow */
202
203         irq = irq_of_parse_and_map(np, 0);
204         if (irq  == NO_IRQ) {
205                 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
206                 return;
207         }
208
209         rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
210         if (rc) {
211                 printk(KERN_ERR __FILE__
212                        ": Unable to request ad7414 irq %d = %d\n", irq, rc);
213                 return;
214         }
215 }
216
217 static inline void pika_dtm_check_fan(void __iomem *fpga)
218 {
219         static int fan_state;
220         u32 fan = in_be32(fpga + 0x34) & (1 << 14);
221
222         if (fan_state != fan) {
223                 fan_state = fan;
224                 if (fan)
225                         printk(KERN_WARNING "Fan rotation error detected."
226                                    " Please check hardware.\n");
227         }
228 }
229
230 static int pika_dtm_thread(void __iomem *fpga)
231 {
232         struct device_node *np;
233         struct i2c_client *client;
234
235         np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
236         if (np == NULL)
237                 return -ENOENT;
238
239         client = of_find_i2c_device_by_node(np);
240         if (client == NULL) {
241                 of_node_put(np);
242                 return -ENOENT;
243         }
244
245         pika_setup_critical_temp(np, client);
246
247         of_node_put(np);
248
249         printk(KERN_INFO "Warp DTM thread running.\n");
250
251         while (!kthread_should_stop()) {
252                 int val;
253
254                 val = i2c_smbus_read_word_data(client, 0);
255                 if (val < 0)
256                         dev_dbg(&client->dev, "DTM read temp failed.\n");
257                 else {
258                         s16 temp = swab16(val);
259                         out_be32(fpga + 0x20, temp);
260                 }
261
262                 pika_dtm_check_fan(fpga);
263
264                 set_current_state(TASK_INTERRUPTIBLE);
265                 schedule_timeout(HZ);
266         }
267
268         return 0;
269 }
270
271 static int __init pika_dtm_start(void)
272 {
273         struct task_struct *dtm_thread;
274         struct device_node *np;
275
276         np = of_find_compatible_node(NULL, NULL, "pika,fpga");
277         if (np == NULL)
278                 return -ENOENT;
279
280         dtm_fpga = of_iomap(np, 0);
281         of_node_put(np);
282         if (dtm_fpga == NULL)
283                 return -ENOENT;
284
285         /* Must get post info before thread starts. */
286         warp_post_info();
287
288         dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
289         if (IS_ERR(dtm_thread)) {
290                 iounmap(dtm_fpga);
291                 return PTR_ERR(dtm_thread);
292         }
293
294         return 0;
295 }
296 machine_late_initcall(warp, pika_dtm_start);
297
298 #else /* !CONFIG_SENSORS_AD7414 */
299
300 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
301 {
302         return 0;
303 }
304
305 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
306 {
307         return 0;
308 }
309
310 machine_late_initcall(warp, warp_post_info);
311
312 #endif
313
314 EXPORT_SYMBOL(pika_dtm_register_shutdown);
315 EXPORT_SYMBOL(pika_dtm_unregister_shutdown);