1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PIKA Warp(tm) board specific routines
5 * Copyright (c) 2008-2009 PIKA Technologies
6 * Sean MacLennan <smaclennan@pikatech.com>
9 #include <linux/init.h>
10 #include <linux/of_platform.h>
11 #include <linux/kthread.h>
12 #include <linux/leds.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/export.h>
22 #include <asm/machdep.h>
26 #include <asm/ppc4xx.h>
30 static const struct of_device_id warp_of_bus[] __initconst = {
31 { .compatible = "ibm,plb4", },
32 { .compatible = "ibm,opb", },
33 { .compatible = "ibm,ebc", },
37 static int __init warp_device_probe(void)
39 of_platform_bus_probe(NULL, warp_of_bus, NULL);
42 machine_device_initcall(warp, warp_device_probe);
44 define_machine(warp) {
46 .compatible = "pika,warp",
47 .progress = udbg_progress,
48 .init_IRQ = uic_init_tree,
49 .get_irq = uic_get_irq,
50 .restart = ppc4xx_reset_system,
54 static int __init warp_post_info(void)
56 struct device_node *np;
60 /* Sighhhh... POST information is in the sd area. */
61 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
65 fpga = of_iomap(np, 0);
70 post1 = in_be32(fpga + 0x40);
71 post2 = in_be32(fpga + 0x44);
76 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
78 printk(KERN_INFO "Warp POST OK\n");
84 #ifdef CONFIG_SENSORS_AD7414
86 static LIST_HEAD(dtm_shutdown_list);
87 static void __iomem *dtm_fpga;
90 struct list_head list;
91 void (*func)(void *arg);
95 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
97 struct dtm_shutdown *shutdown;
99 shutdown = kmalloc(sizeof(struct dtm_shutdown), GFP_KERNEL);
100 if (shutdown == NULL)
103 shutdown->func = func;
106 list_add(&shutdown->list, &dtm_shutdown_list);
111 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
113 struct dtm_shutdown *shutdown;
115 list_for_each_entry(shutdown, &dtm_shutdown_list, list)
116 if (shutdown->func == func && shutdown->arg == arg) {
117 list_del(&shutdown->list);
125 #define WARP_GREEN_LED 0
126 #define WARP_RED_LED 1
128 static struct gpio_led warp_gpio_led_pins[] = {
131 .default_state = LEDS_DEFSTATE_KEEP,
132 .gpiod = NULL, /* to be filled by pika_setup_leds() */
136 .default_state = LEDS_DEFSTATE_KEEP,
137 .gpiod = NULL, /* to be filled by pika_setup_leds() */
141 static struct gpio_led_platform_data warp_gpio_led_data = {
142 .leds = warp_gpio_led_pins,
143 .num_leds = ARRAY_SIZE(warp_gpio_led_pins),
146 static struct platform_device warp_gpio_leds = {
150 .platform_data = &warp_gpio_led_data,
154 static irqreturn_t temp_isr(int irq, void *context)
156 struct dtm_shutdown *shutdown;
161 gpiod_set_value(warp_gpio_led_pins[WARP_GREEN_LED].gpiod, 0);
163 /* Run through the shutdown list. */
164 list_for_each_entry(shutdown, &dtm_shutdown_list, list)
165 shutdown->func(shutdown->arg);
167 printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
171 unsigned reset = in_be32(dtm_fpga + 0x14);
172 out_be32(dtm_fpga + 0x14, reset);
175 gpiod_set_value(warp_gpio_led_pins[WARP_RED_LED].gpiod, value);
185 * Because green and red power LEDs are normally driven by leds-gpio driver,
186 * but in case of critical temperature shutdown we want to drive them
187 * ourselves, we acquire both and then create leds-gpio platform device
188 * ourselves, instead of doing it through device tree. This way we can still
189 * keep access to the gpios and use them when needed.
191 static int pika_setup_leds(void)
193 struct device_node *np, *child;
194 struct gpio_desc *gpio;
195 struct gpio_led *led;
200 np = of_find_compatible_node(NULL, NULL, "warp-power-leds");
202 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
206 for_each_child_of_node(np, child) {
207 for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
208 led = &warp_gpio_led_pins[i];
210 if (!of_node_name_eq(child, led->name))
214 printk(KERN_ERR __FILE__ ": %s led has already been defined\n",
219 gpio = fwnode_gpiod_get_index(of_fwnode_handle(child),
222 error = PTR_ERR_OR_ZERO(gpio);
224 printk(KERN_ERR __FILE__ ": Failed to get %s led gpio: %d\n",
227 goto err_cleanup_pins;
237 /* Skip device registration if no leds have been defined */
239 error = platform_device_register(&warp_gpio_leds);
241 printk(KERN_ERR __FILE__ ": Unable to add leds-gpio: %d\n",
243 goto err_cleanup_pins;
250 for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
251 led = &warp_gpio_led_pins[i];
252 gpiod_put(led->gpiod);
258 static void pika_setup_critical_temp(struct device_node *np,
259 struct i2c_client *client)
263 /* Do this before enabling critical temp interrupt since we
264 * may immediately interrupt.
268 /* These registers are in 1 degree increments. */
269 i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
270 i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
272 irq = irq_of_parse_and_map(np, 0);
274 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
278 rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
280 printk(KERN_ERR __FILE__
281 ": Unable to request ad7414 irq %d = %d\n", irq, rc);
286 static inline void pika_dtm_check_fan(void __iomem *fpga)
288 static int fan_state;
289 u32 fan = in_be32(fpga + 0x34) & (1 << 14);
291 if (fan_state != fan) {
294 printk(KERN_WARNING "Fan rotation error detected."
295 " Please check hardware.\n");
299 static int pika_dtm_thread(void __iomem *fpga)
301 struct device_node *np;
302 struct i2c_client *client;
304 np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
308 client = of_find_i2c_device_by_node(np);
309 if (client == NULL) {
314 pika_setup_critical_temp(np, client);
318 printk(KERN_INFO "Warp DTM thread running.\n");
320 while (!kthread_should_stop()) {
323 val = i2c_smbus_read_word_data(client, 0);
325 dev_dbg(&client->dev, "DTM read temp failed.\n");
327 s16 temp = swab16(val);
328 out_be32(fpga + 0x20, temp);
331 pika_dtm_check_fan(fpga);
333 set_current_state(TASK_INTERRUPTIBLE);
334 schedule_timeout(HZ);
340 static int __init pika_dtm_start(void)
342 struct task_struct *dtm_thread;
343 struct device_node *np;
345 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
349 dtm_fpga = of_iomap(np, 0);
351 if (dtm_fpga == NULL)
354 /* Must get post info before thread starts. */
357 dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
358 if (IS_ERR(dtm_thread)) {
360 return PTR_ERR(dtm_thread);
365 machine_late_initcall(warp, pika_dtm_start);
367 #else /* !CONFIG_SENSORS_AD7414 */
369 int pika_dtm_register_shutdown(void (*func)(void *arg), void *arg)
374 int pika_dtm_unregister_shutdown(void (*func)(void *arg), void *arg)
379 machine_late_initcall(warp, warp_post_info);
383 EXPORT_SYMBOL(pika_dtm_register_shutdown);
384 EXPORT_SYMBOL(pika_dtm_unregister_shutdown);