powerpc: Update Warp to use leds-gpio driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / platforms / 44x / warp.c
blobc5118802a281a776b7a3e23f9dddcbf8e36ea75a
1 /*
2 * PIKA Warp(tm) board specific routines
4 * Copyright (c) 2008-2009 PIKA Technologies
5 * Sean MacLennan <smaclennan@pikatech.com>
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.
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>
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>
28 static __initdata struct of_device_id warp_of_bus[] = {
29 { .compatible = "ibm,plb4", },
30 { .compatible = "ibm,opb", },
31 { .compatible = "ibm,ebc", },
32 {},
35 static int __init warp_device_probe(void)
37 of_platform_bus_probe(NULL, warp_of_bus, NULL);
38 return 0;
40 machine_device_initcall(warp, warp_device_probe);
42 static int __init warp_probe(void)
44 unsigned long root = of_get_flat_dt_root();
46 return of_flat_dt_is_compatible(root, "pika,warp");
49 define_machine(warp) {
50 .name = "Warp",
51 .probe = warp_probe,
52 .progress = udbg_progress,
53 .init_IRQ = uic_init_tree,
54 .get_irq = uic_get_irq,
55 .restart = ppc4xx_reset_system,
56 .calibrate_decr = generic_calibrate_decr,
60 static u32 post_info;
62 /* I am not sure this is the best place for this... */
63 static int __init warp_post_info(void)
65 struct device_node *np;
66 void __iomem *fpga;
67 u32 post1, post2;
69 /* Sighhhh... POST information is in the sd area. */
70 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
71 if (np == NULL)
72 return -ENOENT;
74 fpga = of_iomap(np, 0);
75 of_node_put(np);
76 if (fpga == NULL)
77 return -ENOENT;
79 post1 = in_be32(fpga + 0x40);
80 post2 = in_be32(fpga + 0x44);
82 iounmap(fpga);
84 if (post1 || post2) {
85 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
86 post_info = 1;
87 } else
88 printk(KERN_INFO "Warp POST OK\n");
90 return 0;
94 #ifdef CONFIG_SENSORS_AD7414
96 static LIST_HEAD(dtm_shutdown_list);
97 static void __iomem *dtm_fpga;
98 static unsigned green_led, red_led;
101 struct dtm_shutdown {
102 struct list_head list;
103 void (*func)(void *arg);
104 void *arg;
108 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
110 struct dtm_shutdown *shutdown;
112 shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
113 if (shutdown == NULL)
114 return -ENOMEM;
116 shutdown->func = func;
117 shutdown->arg = arg;
119 list_add(&shutdown->list, &dtm_shutdown_list);
121 return 0;
124 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
126 struct dtm_shutdown *shutdown;
128 list_for_each_entry(shutdown, &dtm_shutdown_list, list)
129 if (shutdown->func == func && shutdown->arg == arg) {
130 list_del(&shutdown->list);
131 kfree(shutdown);
132 return 0;
135 return -EINVAL;
138 static irqreturn_t temp_isr(int irq, void *context)
140 struct dtm_shutdown *shutdown;
141 int value = 1;
143 local_irq_disable();
145 gpio_set_value(green_led, 0);
147 /* Run through the shutdown list. */
148 list_for_each_entry(shutdown, &dtm_shutdown_list, list)
149 shutdown->func(shutdown->arg);
151 printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
153 while (1) {
154 if (dtm_fpga) {
155 unsigned reset = in_be32(dtm_fpga + 0x14);
156 out_be32(dtm_fpga + 0x14, reset);
159 gpio_set_value(red_led, value);
160 value ^= 1;
161 mdelay(500);
165 static int pika_setup_leds(void)
167 struct device_node *np, *child;
169 np = of_find_compatible_node(NULL, NULL, "gpio-leds");
170 if (!np) {
171 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
172 return -ENOENT;
175 for_each_child_of_node(np, child)
176 if (strcmp(child->name, "green") == 0) {
177 green_led = of_get_gpio(child, 0);
178 /* Turn back on the green LED */
179 gpio_set_value(green_led, 1);
180 } else if (strcmp(child->name, "red") == 0) {
181 red_led = of_get_gpio(child, 0);
182 /* Set based on post */
183 gpio_set_value(red_led, post_info);
186 of_node_put(np);
188 return 0;
191 static void pika_setup_critical_temp(struct i2c_client *client)
193 struct device_node *np;
194 int irq, rc;
196 /* Do this before enabling critical temp interrupt since we
197 * may immediately interrupt.
199 pika_setup_leds();
201 /* These registers are in 1 degree increments. */
202 i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
203 i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
205 np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
206 if (np == NULL) {
207 printk(KERN_ERR __FILE__ ": Unable to find ad7414\n");
208 return;
211 irq = irq_of_parse_and_map(np, 0);
212 of_node_put(np);
213 if (irq == NO_IRQ) {
214 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
215 return;
218 rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
219 if (rc) {
220 printk(KERN_ERR __FILE__
221 ": Unable to request ad7414 irq %d = %d\n", irq, rc);
222 return;
226 static inline void pika_dtm_check_fan(void __iomem *fpga)
228 static int fan_state;
229 u32 fan = in_be32(fpga + 0x34) & (1 << 14);
231 if (fan_state != fan) {
232 fan_state = fan;
233 if (fan)
234 printk(KERN_WARNING "Fan rotation error detected."
235 " Please check hardware.\n");
239 static int pika_dtm_thread(void __iomem *fpga)
241 struct i2c_adapter *adap;
242 struct i2c_client *client;
244 /* We loop in case either driver was compiled as a module and
245 * has not been insmoded yet.
247 while (!(adap = i2c_get_adapter(0))) {
248 set_current_state(TASK_INTERRUPTIBLE);
249 schedule_timeout(HZ);
252 while (1) {
253 list_for_each_entry(client, &adap->clients, list)
254 if (client->addr == 0x4a)
255 goto found_it;
257 set_current_state(TASK_INTERRUPTIBLE);
258 schedule_timeout(HZ);
261 found_it:
262 pika_setup_critical_temp(client);
264 i2c_put_adapter(adap);
266 printk(KERN_INFO "PIKA DTM thread running.\n");
268 while (!kthread_should_stop()) {
269 int val;
271 val = i2c_smbus_read_word_data(client, 0);
272 if (val < 0)
273 dev_dbg(&client->dev, "DTM read temp failed.\n");
274 else {
275 s16 temp = swab16(val);
276 out_be32(fpga + 0x20, temp);
279 pika_dtm_check_fan(fpga);
281 set_current_state(TASK_INTERRUPTIBLE);
282 schedule_timeout(HZ);
285 return 0;
289 static int __init pika_dtm_start(void)
291 struct task_struct *dtm_thread;
292 struct device_node *np;
294 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
295 if (np == NULL)
296 return -ENOENT;
298 dtm_fpga = of_iomap(np, 0);
299 of_node_put(np);
300 if (dtm_fpga == NULL)
301 return -ENOENT;
303 /* Must get post info before thread starts. */
304 warp_post_info();
306 dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
307 if (IS_ERR(dtm_thread)) {
308 iounmap(dtm_fpga);
309 return PTR_ERR(dtm_thread);
312 return 0;
314 machine_late_initcall(warp, pika_dtm_start);
316 #else /* !CONFIG_SENSORS_AD7414 */
318 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
320 return 0;
323 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
325 return 0;
328 machine_late_initcall(warp, warp_post_info);
330 #endif
332 EXPORT_SYMBOL(pika_dtm_register_shutdown);
333 EXPORT_SYMBOL(pika_dtm_unregister_shutdown);