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 void __iomem *dtm_fpga;
88 #define WARP_GREEN_LED 0
89 #define WARP_RED_LED 1
91 static struct gpio_led warp_gpio_led_pins[] = {
94 .default_state = LEDS_DEFSTATE_KEEP,
95 .gpiod = NULL, /* to be filled by pika_setup_leds() */
99 .default_state = LEDS_DEFSTATE_KEEP,
100 .gpiod = NULL, /* to be filled by pika_setup_leds() */
104 static struct gpio_led_platform_data warp_gpio_led_data = {
105 .leds = warp_gpio_led_pins,
106 .num_leds = ARRAY_SIZE(warp_gpio_led_pins),
109 static struct platform_device warp_gpio_leds = {
113 .platform_data = &warp_gpio_led_data,
117 static irqreturn_t temp_isr(int irq, void *context)
123 gpiod_set_value(warp_gpio_led_pins[WARP_GREEN_LED].gpiod, 0);
125 printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
129 unsigned reset = in_be32(dtm_fpga + 0x14);
130 out_be32(dtm_fpga + 0x14, reset);
133 gpiod_set_value(warp_gpio_led_pins[WARP_RED_LED].gpiod, value);
143 * Because green and red power LEDs are normally driven by leds-gpio driver,
144 * but in case of critical temperature shutdown we want to drive them
145 * ourselves, we acquire both and then create leds-gpio platform device
146 * ourselves, instead of doing it through device tree. This way we can still
147 * keep access to the gpios and use them when needed.
149 static int pika_setup_leds(void)
151 struct device_node *np, *child;
152 struct gpio_desc *gpio;
153 struct gpio_led *led;
158 np = of_find_compatible_node(NULL, NULL, "warp-power-leds");
160 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
164 for_each_child_of_node(np, child) {
165 for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
166 led = &warp_gpio_led_pins[i];
168 if (!of_node_name_eq(child, led->name))
172 printk(KERN_ERR __FILE__ ": %s led has already been defined\n",
177 gpio = fwnode_gpiod_get_index(of_fwnode_handle(child),
180 error = PTR_ERR_OR_ZERO(gpio);
182 printk(KERN_ERR __FILE__ ": Failed to get %s led gpio: %d\n",
185 goto err_cleanup_pins;
195 /* Skip device registration if no leds have been defined */
197 error = platform_device_register(&warp_gpio_leds);
199 printk(KERN_ERR __FILE__ ": Unable to add leds-gpio: %d\n",
201 goto err_cleanup_pins;
208 for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
209 led = &warp_gpio_led_pins[i];
210 gpiod_put(led->gpiod);
216 static void pika_setup_critical_temp(struct device_node *np,
217 struct i2c_client *client)
221 /* Do this before enabling critical temp interrupt since we
222 * may immediately interrupt.
226 /* These registers are in 1 degree increments. */
227 i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
228 i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
230 irq = irq_of_parse_and_map(np, 0);
232 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
236 rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
238 printk(KERN_ERR __FILE__
239 ": Unable to request ad7414 irq %d = %d\n", irq, rc);
244 static inline void pika_dtm_check_fan(void __iomem *fpga)
246 static int fan_state;
247 u32 fan = in_be32(fpga + 0x34) & (1 << 14);
249 if (fan_state != fan) {
252 printk(KERN_WARNING "Fan rotation error detected."
253 " Please check hardware.\n");
257 static int pika_dtm_thread(void __iomem *fpga)
259 struct device_node *np;
260 struct i2c_client *client;
262 np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
266 client = of_find_i2c_device_by_node(np);
267 if (client == NULL) {
272 pika_setup_critical_temp(np, client);
276 printk(KERN_INFO "Warp DTM thread running.\n");
278 while (!kthread_should_stop()) {
281 val = i2c_smbus_read_word_data(client, 0);
283 dev_dbg(&client->dev, "DTM read temp failed.\n");
285 s16 temp = swab16(val);
286 out_be32(fpga + 0x20, temp);
289 pika_dtm_check_fan(fpga);
291 set_current_state(TASK_INTERRUPTIBLE);
292 schedule_timeout(HZ);
298 static int __init pika_dtm_start(void)
300 struct task_struct *dtm_thread;
301 struct device_node *np;
303 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
307 dtm_fpga = of_iomap(np, 0);
309 if (dtm_fpga == NULL)
312 /* Must get post info before thread starts. */
315 dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
316 if (IS_ERR(dtm_thread)) {
318 return PTR_ERR(dtm_thread);
323 machine_late_initcall(warp, pika_dtm_start);
325 #else /* !CONFIG_SENSORS_AD7414 */
327 machine_late_initcall(warp, warp_post_info);