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/platform_device.h>
12 #include <linux/kthread.h>
13 #include <linux/leds.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
23 #include <asm/machdep.h>
27 #include <asm/ppc4xx.h>
31 static const struct of_device_id warp_of_bus[] __initconst = {
32 { .compatible = "ibm,plb4", },
33 { .compatible = "ibm,opb", },
34 { .compatible = "ibm,ebc", },
38 static int __init warp_device_probe(void)
40 of_platform_bus_probe(NULL, warp_of_bus, NULL);
43 machine_device_initcall(warp, warp_device_probe);
45 define_machine(warp) {
47 .compatible = "pika,warp",
48 .progress = udbg_progress,
49 .init_IRQ = uic_init_tree,
50 .get_irq = uic_get_irq,
51 .restart = ppc4xx_reset_system,
55 static int __init warp_post_info(void)
57 struct device_node *np;
61 /* Sighhhh... POST information is in the sd area. */
62 np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
66 fpga = of_iomap(np, 0);
71 post1 = in_be32(fpga + 0x40);
72 post2 = in_be32(fpga + 0x44);
77 printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
79 printk(KERN_INFO "Warp POST OK\n");
85 #ifdef CONFIG_SENSORS_AD7414
87 static void __iomem *dtm_fpga;
89 #define WARP_GREEN_LED 0
90 #define WARP_RED_LED 1
92 static struct gpio_led warp_gpio_led_pins[] = {
95 .default_state = LEDS_DEFSTATE_KEEP,
96 .gpiod = NULL, /* to be filled by pika_setup_leds() */
100 .default_state = LEDS_DEFSTATE_KEEP,
101 .gpiod = NULL, /* to be filled by pika_setup_leds() */
105 static struct gpio_led_platform_data warp_gpio_led_data = {
106 .leds = warp_gpio_led_pins,
107 .num_leds = ARRAY_SIZE(warp_gpio_led_pins),
110 static struct platform_device warp_gpio_leds = {
114 .platform_data = &warp_gpio_led_data,
118 static irqreturn_t temp_isr(int irq, void *context)
124 gpiod_set_value(warp_gpio_led_pins[WARP_GREEN_LED].gpiod, 0);
126 printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
130 unsigned reset = in_be32(dtm_fpga + 0x14);
131 out_be32(dtm_fpga + 0x14, reset);
134 gpiod_set_value(warp_gpio_led_pins[WARP_RED_LED].gpiod, value);
144 * Because green and red power LEDs are normally driven by leds-gpio driver,
145 * but in case of critical temperature shutdown we want to drive them
146 * ourselves, we acquire both and then create leds-gpio platform device
147 * ourselves, instead of doing it through device tree. This way we can still
148 * keep access to the gpios and use them when needed.
150 static int pika_setup_leds(void)
152 struct device_node *np, *child;
153 struct gpio_desc *gpio;
154 struct gpio_led *led;
159 np = of_find_compatible_node(NULL, NULL, "warp-power-leds");
161 printk(KERN_ERR __FILE__ ": Unable to find leds\n");
165 for_each_child_of_node(np, child) {
166 for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
167 led = &warp_gpio_led_pins[i];
169 if (!of_node_name_eq(child, led->name))
173 printk(KERN_ERR __FILE__ ": %s led has already been defined\n",
178 gpio = fwnode_gpiod_get_index(of_fwnode_handle(child),
181 error = PTR_ERR_OR_ZERO(gpio);
183 printk(KERN_ERR __FILE__ ": Failed to get %s led gpio: %d\n",
186 goto err_cleanup_pins;
196 /* Skip device registration if no leds have been defined */
198 error = platform_device_register(&warp_gpio_leds);
200 printk(KERN_ERR __FILE__ ": Unable to add leds-gpio: %d\n",
202 goto err_cleanup_pins;
209 for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
210 led = &warp_gpio_led_pins[i];
211 gpiod_put(led->gpiod);
217 static void pika_setup_critical_temp(struct device_node *np,
218 struct i2c_client *client)
222 /* Do this before enabling critical temp interrupt since we
223 * may immediately interrupt.
227 /* These registers are in 1 degree increments. */
228 i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
229 i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
231 irq = irq_of_parse_and_map(np, 0);
233 printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
237 rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
239 printk(KERN_ERR __FILE__
240 ": Unable to request ad7414 irq %d = %d\n", irq, rc);
245 static inline void pika_dtm_check_fan(void __iomem *fpga)
247 static int fan_state;
248 u32 fan = in_be32(fpga + 0x34) & (1 << 14);
250 if (fan_state != fan) {
253 printk(KERN_WARNING "Fan rotation error detected."
254 " Please check hardware.\n");
258 static int pika_dtm_thread(void __iomem *fpga)
260 struct device_node *np;
261 struct i2c_client *client;
263 np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
267 client = of_find_i2c_device_by_node(np);
268 if (client == NULL) {
273 pika_setup_critical_temp(np, client);
277 printk(KERN_INFO "Warp DTM thread running.\n");
279 while (!kthread_should_stop()) {
282 val = i2c_smbus_read_word_data(client, 0);
284 dev_dbg(&client->dev, "DTM read temp failed.\n");
286 s16 temp = swab16(val);
287 out_be32(fpga + 0x20, temp);
290 pika_dtm_check_fan(fpga);
292 set_current_state(TASK_INTERRUPTIBLE);
293 schedule_timeout(HZ);
299 static int __init pika_dtm_start(void)
301 struct task_struct *dtm_thread;
302 struct device_node *np;
304 np = of_find_compatible_node(NULL, NULL, "pika,fpga");
308 dtm_fpga = of_iomap(np, 0);
310 if (dtm_fpga == NULL)
313 /* Must get post info before thread starts. */
316 dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
317 if (IS_ERR(dtm_thread)) {
319 return PTR_ERR(dtm_thread);
324 machine_late_initcall(warp, pika_dtm_start);
326 #else /* !CONFIG_SENSORS_AD7414 */
328 machine_late_initcall(warp, warp_post_info);