18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Windfarm PowerMac thermal control. LM87 sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2012 Benjamin Herrenschmidt, IBM Corp. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/types.h> 98c2ecf20Sopenharmony_ci#include <linux/errno.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/delay.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/wait.h> 158c2ecf20Sopenharmony_ci#include <linux/i2c.h> 168c2ecf20Sopenharmony_ci#include <asm/prom.h> 178c2ecf20Sopenharmony_ci#include <asm/machdep.h> 188c2ecf20Sopenharmony_ci#include <asm/io.h> 198c2ecf20Sopenharmony_ci#include <asm/sections.h> 208c2ecf20Sopenharmony_ci#include <asm/pmac_low_i2c.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "windfarm.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define VERSION "1.0" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#undef DEBUG 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#ifdef DEBUG 298c2ecf20Sopenharmony_ci#define DBG(args...) printk(args) 308c2ecf20Sopenharmony_ci#else 318c2ecf20Sopenharmony_ci#define DBG(args...) do { } while(0) 328c2ecf20Sopenharmony_ci#endif 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistruct wf_lm87_sensor { 358c2ecf20Sopenharmony_ci struct i2c_client *i2c; 368c2ecf20Sopenharmony_ci struct wf_sensor sens; 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci#define wf_to_lm87(c) container_of(c, struct wf_lm87_sensor, sens) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic int wf_lm87_read_reg(struct i2c_client *chip, int reg) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci int rc, tries = 0; 448c2ecf20Sopenharmony_ci u8 buf; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci for (;;) { 478c2ecf20Sopenharmony_ci /* Set address */ 488c2ecf20Sopenharmony_ci buf = (u8)reg; 498c2ecf20Sopenharmony_ci rc = i2c_master_send(chip, &buf, 1); 508c2ecf20Sopenharmony_ci if (rc <= 0) 518c2ecf20Sopenharmony_ci goto error; 528c2ecf20Sopenharmony_ci rc = i2c_master_recv(chip, &buf, 1); 538c2ecf20Sopenharmony_ci if (rc <= 0) 548c2ecf20Sopenharmony_ci goto error; 558c2ecf20Sopenharmony_ci return (int)buf; 568c2ecf20Sopenharmony_ci error: 578c2ecf20Sopenharmony_ci DBG("wf_lm87: Error reading LM87, retrying...\n"); 588c2ecf20Sopenharmony_ci if (++tries > 10) { 598c2ecf20Sopenharmony_ci printk(KERN_ERR "wf_lm87: Error reading LM87 !\n"); 608c2ecf20Sopenharmony_ci return -EIO; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci msleep(10); 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic int wf_lm87_get(struct wf_sensor *sr, s32 *value) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct wf_lm87_sensor *lm = sr->priv; 698c2ecf20Sopenharmony_ci s32 temp; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (lm->i2c == NULL) 728c2ecf20Sopenharmony_ci return -ENODEV; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci#define LM87_INT_TEMP 0x27 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* Read temperature register */ 778c2ecf20Sopenharmony_ci temp = wf_lm87_read_reg(lm->i2c, LM87_INT_TEMP); 788c2ecf20Sopenharmony_ci if (temp < 0) 798c2ecf20Sopenharmony_ci return temp; 808c2ecf20Sopenharmony_ci *value = temp << 16; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci return 0; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic void wf_lm87_release(struct wf_sensor *sr) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci struct wf_lm87_sensor *lm = wf_to_lm87(sr); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci kfree(lm); 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic const struct wf_sensor_ops wf_lm87_ops = { 938c2ecf20Sopenharmony_ci .get_value = wf_lm87_get, 948c2ecf20Sopenharmony_ci .release = wf_lm87_release, 958c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 968c2ecf20Sopenharmony_ci}; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic int wf_lm87_probe(struct i2c_client *client, 998c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct wf_lm87_sensor *lm; 1028c2ecf20Sopenharmony_ci const char *name = NULL, *loc; 1038c2ecf20Sopenharmony_ci struct device_node *np = NULL; 1048c2ecf20Sopenharmony_ci int rc; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci /* 1078c2ecf20Sopenharmony_ci * The lm87 contains a whole pile of sensors, additionally, 1088c2ecf20Sopenharmony_ci * the Xserve G5 has several lm87's. However, for now we only 1098c2ecf20Sopenharmony_ci * care about the internal temperature sensor 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_ci for_each_child_of_node(client->dev.of_node, np) { 1128c2ecf20Sopenharmony_ci if (!of_node_name_eq(np, "int-temp")) 1138c2ecf20Sopenharmony_ci continue; 1148c2ecf20Sopenharmony_ci loc = of_get_property(np, "location", NULL); 1158c2ecf20Sopenharmony_ci if (!loc) 1168c2ecf20Sopenharmony_ci continue; 1178c2ecf20Sopenharmony_ci if (strstr(loc, "DIMM")) 1188c2ecf20Sopenharmony_ci name = "dimms-temp"; 1198c2ecf20Sopenharmony_ci else if (strstr(loc, "Processors")) 1208c2ecf20Sopenharmony_ci name = "between-cpus-temp"; 1218c2ecf20Sopenharmony_ci if (name) { 1228c2ecf20Sopenharmony_ci of_node_put(np); 1238c2ecf20Sopenharmony_ci break; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci if (!name) { 1278c2ecf20Sopenharmony_ci pr_warn("wf_lm87: Unsupported sensor %pOF\n", 1288c2ecf20Sopenharmony_ci client->dev.of_node); 1298c2ecf20Sopenharmony_ci return -ENODEV; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci lm = kzalloc(sizeof(struct wf_lm87_sensor), GFP_KERNEL); 1338c2ecf20Sopenharmony_ci if (lm == NULL) 1348c2ecf20Sopenharmony_ci return -ENODEV; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci lm->i2c = client; 1378c2ecf20Sopenharmony_ci lm->sens.name = name; 1388c2ecf20Sopenharmony_ci lm->sens.ops = &wf_lm87_ops; 1398c2ecf20Sopenharmony_ci lm->sens.priv = lm; 1408c2ecf20Sopenharmony_ci i2c_set_clientdata(client, lm); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci rc = wf_register_sensor(&lm->sens); 1438c2ecf20Sopenharmony_ci if (rc) 1448c2ecf20Sopenharmony_ci kfree(lm); 1458c2ecf20Sopenharmony_ci return rc; 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic int wf_lm87_remove(struct i2c_client *client) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct wf_lm87_sensor *lm = i2c_get_clientdata(client); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci /* Mark client detached */ 1538c2ecf20Sopenharmony_ci lm->i2c = NULL; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci /* release sensor */ 1568c2ecf20Sopenharmony_ci wf_unregister_sensor(&lm->sens); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci return 0; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic const struct i2c_device_id wf_lm87_id[] = { 1628c2ecf20Sopenharmony_ci { "MAC,lm87cimt", 0 }, 1638c2ecf20Sopenharmony_ci { } 1648c2ecf20Sopenharmony_ci}; 1658c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, wf_lm87_id); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic const struct of_device_id wf_lm87_of_id[] = { 1688c2ecf20Sopenharmony_ci { .compatible = "lm87cimt", }, 1698c2ecf20Sopenharmony_ci { } 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, wf_lm87_of_id); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic struct i2c_driver wf_lm87_driver = { 1748c2ecf20Sopenharmony_ci .driver = { 1758c2ecf20Sopenharmony_ci .name = "wf_lm87", 1768c2ecf20Sopenharmony_ci .of_match_table = wf_lm87_of_id, 1778c2ecf20Sopenharmony_ci }, 1788c2ecf20Sopenharmony_ci .probe = wf_lm87_probe, 1798c2ecf20Sopenharmony_ci .remove = wf_lm87_remove, 1808c2ecf20Sopenharmony_ci .id_table = wf_lm87_id, 1818c2ecf20Sopenharmony_ci}; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic int __init wf_lm87_sensor_init(void) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci /* We only support this on the Xserve */ 1868c2ecf20Sopenharmony_ci if (!of_machine_is_compatible("RackMac3,1")) 1878c2ecf20Sopenharmony_ci return -ENODEV; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci return i2c_add_driver(&wf_lm87_driver); 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic void __exit wf_lm87_sensor_exit(void) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci i2c_del_driver(&wf_lm87_driver); 1958c2ecf20Sopenharmony_ci} 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cimodule_init(wf_lm87_sensor_init); 1998c2ecf20Sopenharmony_cimodule_exit(wf_lm87_sensor_exit); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ciMODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); 2028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("LM87 sensor objects for PowerMacs thermal control"); 2038c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2048c2ecf20Sopenharmony_ci 205