1a5b79d62Sstigge@antcom.de /* 2a5b79d62Sstigge@antcom.de * max6639.c - Support for Maxim MAX6639 3a5b79d62Sstigge@antcom.de * 4a5b79d62Sstigge@antcom.de * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller 5a5b79d62Sstigge@antcom.de * 6a5b79d62Sstigge@antcom.de * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> 7a5b79d62Sstigge@antcom.de * 8a5b79d62Sstigge@antcom.de * based on the initial MAX6639 support from semptian.net 9a5b79d62Sstigge@antcom.de * by He Changqing <hechangqing@semptian.com> 10a5b79d62Sstigge@antcom.de * 11a5b79d62Sstigge@antcom.de * This program is free software; you can redistribute it and/or modify 12a5b79d62Sstigge@antcom.de * it under the terms of the GNU General Public License as published by 13a5b79d62Sstigge@antcom.de * the Free Software Foundation; either version 2 of the License, or 14a5b79d62Sstigge@antcom.de * (at your option) any later version. 15a5b79d62Sstigge@antcom.de * 16a5b79d62Sstigge@antcom.de * This program is distributed in the hope that it will be useful, 17a5b79d62Sstigge@antcom.de * but WITHOUT ANY WARRANTY; without even the implied warranty of 18a5b79d62Sstigge@antcom.de * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19a5b79d62Sstigge@antcom.de * GNU General Public License for more details. 20a5b79d62Sstigge@antcom.de * 21a5b79d62Sstigge@antcom.de * You should have received a copy of the GNU General Public License 22a5b79d62Sstigge@antcom.de * along with this program; if not, write to the Free Software 23a5b79d62Sstigge@antcom.de * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24a5b79d62Sstigge@antcom.de */ 25a5b79d62Sstigge@antcom.de 26a5b79d62Sstigge@antcom.de #include <linux/module.h> 27a5b79d62Sstigge@antcom.de #include <linux/init.h> 28a5b79d62Sstigge@antcom.de #include <linux/slab.h> 29a5b79d62Sstigge@antcom.de #include <linux/jiffies.h> 30a5b79d62Sstigge@antcom.de #include <linux/i2c.h> 31a5b79d62Sstigge@antcom.de #include <linux/hwmon.h> 32a5b79d62Sstigge@antcom.de #include <linux/hwmon-sysfs.h> 33a5b79d62Sstigge@antcom.de #include <linux/err.h> 34a5b79d62Sstigge@antcom.de #include <linux/mutex.h> 350c9fe161SWolfram Sang #include <linux/platform_data/max6639.h> 36a5b79d62Sstigge@antcom.de 37a5b79d62Sstigge@antcom.de /* Addresses to scan */ 387edc8cc1SAxel Lin static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END }; 39a5b79d62Sstigge@antcom.de 40a5b79d62Sstigge@antcom.de /* The MAX6639 registers, valid channel numbers: 0, 1 */ 41a5b79d62Sstigge@antcom.de #define MAX6639_REG_TEMP(ch) (0x00 + (ch)) 42a5b79d62Sstigge@antcom.de #define MAX6639_REG_STATUS 0x02 43a5b79d62Sstigge@antcom.de #define MAX6639_REG_OUTPUT_MASK 0x03 44a5b79d62Sstigge@antcom.de #define MAX6639_REG_GCONFIG 0x04 45a5b79d62Sstigge@antcom.de #define MAX6639_REG_TEMP_EXT(ch) (0x05 + (ch)) 46a5b79d62Sstigge@antcom.de #define MAX6639_REG_ALERT_LIMIT(ch) (0x08 + (ch)) 47a5b79d62Sstigge@antcom.de #define MAX6639_REG_OT_LIMIT(ch) (0x0A + (ch)) 48a5b79d62Sstigge@antcom.de #define MAX6639_REG_THERM_LIMIT(ch) (0x0C + (ch)) 49a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG1(ch) (0x10 + (ch) * 4) 50a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG2a(ch) (0x11 + (ch) * 4) 51a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG2b(ch) (0x12 + (ch) * 4) 52a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG3(ch) (0x13 + (ch) * 4) 53a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CNT(ch) (0x20 + (ch)) 54a5b79d62Sstigge@antcom.de #define MAX6639_REG_TARGET_CNT(ch) (0x22 + (ch)) 55a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_PPR(ch) (0x24 + (ch)) 56a5b79d62Sstigge@antcom.de #define MAX6639_REG_TARGTDUTY(ch) (0x26 + (ch)) 57a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_START_TEMP(ch) (0x28 + (ch)) 58a5b79d62Sstigge@antcom.de #define MAX6639_REG_DEVID 0x3D 59a5b79d62Sstigge@antcom.de #define MAX6639_REG_MANUID 0x3E 60a5b79d62Sstigge@antcom.de #define MAX6639_REG_DEVREV 0x3F 61a5b79d62Sstigge@antcom.de 62a5b79d62Sstigge@antcom.de /* Register bits */ 63a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_STANDBY 0x80 64a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_POR 0x40 65a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_DISABLE_TIMEOUT 0x20 66a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_CH2_LOCAL 0x10 67177f3b92Sstigge@antcom.de #define MAX6639_GCONFIG_PWM_FREQ_HI 0x08 68a5b79d62Sstigge@antcom.de 69a5b79d62Sstigge@antcom.de #define MAX6639_FAN_CONFIG1_PWM 0x80 70a5b79d62Sstigge@antcom.de 71177f3b92Sstigge@antcom.de #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED 0x40 72177f3b92Sstigge@antcom.de 73a5b79d62Sstigge@antcom.de static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 }; 74a5b79d62Sstigge@antcom.de 75b63d97a3SChris D Schimp #define FAN_FROM_REG(val, rpm_range) ((val) == 0 || (val) == 255 ? \ 76b63d97a3SChris D Schimp 0 : (rpm_ranges[rpm_range] * 30) / (val)) 772a844c14SGuenter Roeck #define TEMP_LIMIT_TO_REG(val) clamp_val((val) / 1000, 0, 255) 78a5b79d62Sstigge@antcom.de 79a5b79d62Sstigge@antcom.de /* 80a5b79d62Sstigge@antcom.de * Client data (each client gets its own) 81a5b79d62Sstigge@antcom.de */ 82a5b79d62Sstigge@antcom.de struct max6639_data { 837981c584SGuenter Roeck struct i2c_client *client; 84a5b79d62Sstigge@antcom.de struct mutex update_lock; 85a5b79d62Sstigge@antcom.de char valid; /* !=0 if following fields are valid */ 86a5b79d62Sstigge@antcom.de unsigned long last_updated; /* In jiffies */ 87a5b79d62Sstigge@antcom.de 88a5b79d62Sstigge@antcom.de /* Register values sampled regularly */ 89a5b79d62Sstigge@antcom.de u16 temp[2]; /* Temperature, in 1/8 C, 0..255 C */ 90a5b79d62Sstigge@antcom.de bool temp_fault[2]; /* Detected temperature diode failure */ 91a5b79d62Sstigge@antcom.de u8 fan[2]; /* Register value: TACH count for fans >=30 */ 92a5b79d62Sstigge@antcom.de u8 status; /* Detected channel alarms and fan failures */ 93a5b79d62Sstigge@antcom.de 94a5b79d62Sstigge@antcom.de /* Register values only written to */ 95a5b79d62Sstigge@antcom.de u8 pwm[2]; /* Register value: Duty cycle 0..120 */ 96a5b79d62Sstigge@antcom.de u8 temp_therm[2]; /* THERM Temperature, 0..255 C (->_max) */ 97a5b79d62Sstigge@antcom.de u8 temp_alert[2]; /* ALERT Temperature, 0..255 C (->_crit) */ 98a5b79d62Sstigge@antcom.de u8 temp_ot[2]; /* OT Temperature, 0..255 C (->_emergency) */ 99a5b79d62Sstigge@antcom.de 100a5b79d62Sstigge@antcom.de /* Register values initialized only once */ 101a5b79d62Sstigge@antcom.de u8 ppr; /* Pulses per rotation 0..3 for 1..4 ppr */ 102a5b79d62Sstigge@antcom.de u8 rpm_range; /* Index in above rpm_ranges table */ 103a5b79d62Sstigge@antcom.de }; 104a5b79d62Sstigge@antcom.de 105a5b79d62Sstigge@antcom.de static struct max6639_data *max6639_update_device(struct device *dev) 106a5b79d62Sstigge@antcom.de { 1077981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 1087981c584SGuenter Roeck struct i2c_client *client = data->client; 109a5b79d62Sstigge@antcom.de struct max6639_data *ret = data; 110a5b79d62Sstigge@antcom.de int i; 111a5b79d62Sstigge@antcom.de int status_reg; 112a5b79d62Sstigge@antcom.de 113a5b79d62Sstigge@antcom.de mutex_lock(&data->update_lock); 114a5b79d62Sstigge@antcom.de 115a5b79d62Sstigge@antcom.de if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) { 116a5b79d62Sstigge@antcom.de int res; 117a5b79d62Sstigge@antcom.de 118a5b79d62Sstigge@antcom.de dev_dbg(&client->dev, "Starting max6639 update\n"); 119a5b79d62Sstigge@antcom.de 120a5b79d62Sstigge@antcom.de status_reg = i2c_smbus_read_byte_data(client, 121a5b79d62Sstigge@antcom.de MAX6639_REG_STATUS); 122a5b79d62Sstigge@antcom.de if (status_reg < 0) { 123a5b79d62Sstigge@antcom.de ret = ERR_PTR(status_reg); 124a5b79d62Sstigge@antcom.de goto abort; 125a5b79d62Sstigge@antcom.de } 126a5b79d62Sstigge@antcom.de 127a5b79d62Sstigge@antcom.de data->status = status_reg; 128a5b79d62Sstigge@antcom.de 129a5b79d62Sstigge@antcom.de for (i = 0; i < 2; i++) { 130a5b79d62Sstigge@antcom.de res = i2c_smbus_read_byte_data(client, 131a5b79d62Sstigge@antcom.de MAX6639_REG_FAN_CNT(i)); 132a5b79d62Sstigge@antcom.de if (res < 0) { 133a5b79d62Sstigge@antcom.de ret = ERR_PTR(res); 134a5b79d62Sstigge@antcom.de goto abort; 135a5b79d62Sstigge@antcom.de } 136a5b79d62Sstigge@antcom.de data->fan[i] = res; 137a5b79d62Sstigge@antcom.de 138a5b79d62Sstigge@antcom.de res = i2c_smbus_read_byte_data(client, 139a5b79d62Sstigge@antcom.de MAX6639_REG_TEMP_EXT(i)); 140a5b79d62Sstigge@antcom.de if (res < 0) { 141a5b79d62Sstigge@antcom.de ret = ERR_PTR(res); 142a5b79d62Sstigge@antcom.de goto abort; 143a5b79d62Sstigge@antcom.de } 144a5b79d62Sstigge@antcom.de data->temp[i] = res >> 5; 145a5b79d62Sstigge@antcom.de data->temp_fault[i] = res & 0x01; 146a5b79d62Sstigge@antcom.de 147a5b79d62Sstigge@antcom.de res = i2c_smbus_read_byte_data(client, 148a5b79d62Sstigge@antcom.de MAX6639_REG_TEMP(i)); 149a5b79d62Sstigge@antcom.de if (res < 0) { 150a5b79d62Sstigge@antcom.de ret = ERR_PTR(res); 151a5b79d62Sstigge@antcom.de goto abort; 152a5b79d62Sstigge@antcom.de } 153a5b79d62Sstigge@antcom.de data->temp[i] |= res << 3; 154a5b79d62Sstigge@antcom.de } 155a5b79d62Sstigge@antcom.de 156a5b79d62Sstigge@antcom.de data->last_updated = jiffies; 157a5b79d62Sstigge@antcom.de data->valid = 1; 158a5b79d62Sstigge@antcom.de } 159a5b79d62Sstigge@antcom.de abort: 160a5b79d62Sstigge@antcom.de mutex_unlock(&data->update_lock); 161a5b79d62Sstigge@antcom.de 162a5b79d62Sstigge@antcom.de return ret; 163a5b79d62Sstigge@antcom.de } 164a5b79d62Sstigge@antcom.de 165*0a0ab22aSGuenter Roeck static ssize_t temp_input_show(struct device *dev, 166a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, char *buf) 167a5b79d62Sstigge@antcom.de { 168a5b79d62Sstigge@antcom.de long temp; 169a5b79d62Sstigge@antcom.de struct max6639_data *data = max6639_update_device(dev); 170a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 171a5b79d62Sstigge@antcom.de 172a5b79d62Sstigge@antcom.de if (IS_ERR(data)) 173a5b79d62Sstigge@antcom.de return PTR_ERR(data); 174a5b79d62Sstigge@antcom.de 175a5b79d62Sstigge@antcom.de temp = data->temp[attr->index] * 125; 176a5b79d62Sstigge@antcom.de return sprintf(buf, "%ld\n", temp); 177a5b79d62Sstigge@antcom.de } 178a5b79d62Sstigge@antcom.de 179*0a0ab22aSGuenter Roeck static ssize_t temp_fault_show(struct device *dev, 180a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, char *buf) 181a5b79d62Sstigge@antcom.de { 182a5b79d62Sstigge@antcom.de struct max6639_data *data = max6639_update_device(dev); 183a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 184a5b79d62Sstigge@antcom.de 185a5b79d62Sstigge@antcom.de if (IS_ERR(data)) 186a5b79d62Sstigge@antcom.de return PTR_ERR(data); 187a5b79d62Sstigge@antcom.de 188a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", data->temp_fault[attr->index]); 189a5b79d62Sstigge@antcom.de } 190a5b79d62Sstigge@antcom.de 191*0a0ab22aSGuenter Roeck static ssize_t temp_max_show(struct device *dev, 192a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, char *buf) 193a5b79d62Sstigge@antcom.de { 194a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 1957981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 196a5b79d62Sstigge@antcom.de 197a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000)); 198a5b79d62Sstigge@antcom.de } 199a5b79d62Sstigge@antcom.de 200*0a0ab22aSGuenter Roeck static ssize_t temp_max_store(struct device *dev, 201a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, 202a5b79d62Sstigge@antcom.de const char *buf, size_t count) 203a5b79d62Sstigge@antcom.de { 204a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 2057981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 2067981c584SGuenter Roeck struct i2c_client *client = data->client; 207a5b79d62Sstigge@antcom.de unsigned long val; 208a5b79d62Sstigge@antcom.de int res; 209a5b79d62Sstigge@antcom.de 210179c4fdbSFrans Meulenbroeks res = kstrtoul(buf, 10, &val); 211a5b79d62Sstigge@antcom.de if (res) 212a5b79d62Sstigge@antcom.de return res; 213a5b79d62Sstigge@antcom.de 214a5b79d62Sstigge@antcom.de mutex_lock(&data->update_lock); 215a5b79d62Sstigge@antcom.de data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val); 216a5b79d62Sstigge@antcom.de i2c_smbus_write_byte_data(client, 217a5b79d62Sstigge@antcom.de MAX6639_REG_THERM_LIMIT(attr->index), 218a5b79d62Sstigge@antcom.de data->temp_therm[attr->index]); 219a5b79d62Sstigge@antcom.de mutex_unlock(&data->update_lock); 220a5b79d62Sstigge@antcom.de return count; 221a5b79d62Sstigge@antcom.de } 222a5b79d62Sstigge@antcom.de 223*0a0ab22aSGuenter Roeck static ssize_t temp_crit_show(struct device *dev, 224a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, char *buf) 225a5b79d62Sstigge@antcom.de { 226a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 2277981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 228a5b79d62Sstigge@antcom.de 229a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000)); 230a5b79d62Sstigge@antcom.de } 231a5b79d62Sstigge@antcom.de 232*0a0ab22aSGuenter Roeck static ssize_t temp_crit_store(struct device *dev, 233a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, 234a5b79d62Sstigge@antcom.de const char *buf, size_t count) 235a5b79d62Sstigge@antcom.de { 236a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 2377981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 2387981c584SGuenter Roeck struct i2c_client *client = data->client; 239a5b79d62Sstigge@antcom.de unsigned long val; 240a5b79d62Sstigge@antcom.de int res; 241a5b79d62Sstigge@antcom.de 242179c4fdbSFrans Meulenbroeks res = kstrtoul(buf, 10, &val); 243a5b79d62Sstigge@antcom.de if (res) 244a5b79d62Sstigge@antcom.de return res; 245a5b79d62Sstigge@antcom.de 246a5b79d62Sstigge@antcom.de mutex_lock(&data->update_lock); 247a5b79d62Sstigge@antcom.de data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val); 248a5b79d62Sstigge@antcom.de i2c_smbus_write_byte_data(client, 249a5b79d62Sstigge@antcom.de MAX6639_REG_ALERT_LIMIT(attr->index), 250a5b79d62Sstigge@antcom.de data->temp_alert[attr->index]); 251a5b79d62Sstigge@antcom.de mutex_unlock(&data->update_lock); 252a5b79d62Sstigge@antcom.de return count; 253a5b79d62Sstigge@antcom.de } 254a5b79d62Sstigge@antcom.de 255*0a0ab22aSGuenter Roeck static ssize_t temp_emergency_show(struct device *dev, 256a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, 257a5b79d62Sstigge@antcom.de char *buf) 258a5b79d62Sstigge@antcom.de { 259a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 2607981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 261a5b79d62Sstigge@antcom.de 262a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000)); 263a5b79d62Sstigge@antcom.de } 264a5b79d62Sstigge@antcom.de 265*0a0ab22aSGuenter Roeck static ssize_t temp_emergency_store(struct device *dev, 266a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, 267a5b79d62Sstigge@antcom.de const char *buf, size_t count) 268a5b79d62Sstigge@antcom.de { 269a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 2707981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 2717981c584SGuenter Roeck struct i2c_client *client = data->client; 272a5b79d62Sstigge@antcom.de unsigned long val; 273a5b79d62Sstigge@antcom.de int res; 274a5b79d62Sstigge@antcom.de 275179c4fdbSFrans Meulenbroeks res = kstrtoul(buf, 10, &val); 276a5b79d62Sstigge@antcom.de if (res) 277a5b79d62Sstigge@antcom.de return res; 278a5b79d62Sstigge@antcom.de 279a5b79d62Sstigge@antcom.de mutex_lock(&data->update_lock); 280a5b79d62Sstigge@antcom.de data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val); 281a5b79d62Sstigge@antcom.de i2c_smbus_write_byte_data(client, 282a5b79d62Sstigge@antcom.de MAX6639_REG_OT_LIMIT(attr->index), 283a5b79d62Sstigge@antcom.de data->temp_ot[attr->index]); 284a5b79d62Sstigge@antcom.de mutex_unlock(&data->update_lock); 285a5b79d62Sstigge@antcom.de return count; 286a5b79d62Sstigge@antcom.de } 287a5b79d62Sstigge@antcom.de 288*0a0ab22aSGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr, 289*0a0ab22aSGuenter Roeck char *buf) 290a5b79d62Sstigge@antcom.de { 291a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 2927981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 293a5b79d62Sstigge@antcom.de 294a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120); 295a5b79d62Sstigge@antcom.de } 296a5b79d62Sstigge@antcom.de 297*0a0ab22aSGuenter Roeck static ssize_t pwm_store(struct device *dev, 298*0a0ab22aSGuenter Roeck struct device_attribute *dev_attr, const char *buf, 299*0a0ab22aSGuenter Roeck size_t count) 300a5b79d62Sstigge@antcom.de { 301a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 3027981c584SGuenter Roeck struct max6639_data *data = dev_get_drvdata(dev); 3037981c584SGuenter Roeck struct i2c_client *client = data->client; 304a5b79d62Sstigge@antcom.de unsigned long val; 305a5b79d62Sstigge@antcom.de int res; 306a5b79d62Sstigge@antcom.de 307179c4fdbSFrans Meulenbroeks res = kstrtoul(buf, 10, &val); 308a5b79d62Sstigge@antcom.de if (res) 309a5b79d62Sstigge@antcom.de return res; 310a5b79d62Sstigge@antcom.de 3112a844c14SGuenter Roeck val = clamp_val(val, 0, 255); 312a5b79d62Sstigge@antcom.de 313a5b79d62Sstigge@antcom.de mutex_lock(&data->update_lock); 314a5b79d62Sstigge@antcom.de data->pwm[attr->index] = (u8)(val * 120 / 255); 315a5b79d62Sstigge@antcom.de i2c_smbus_write_byte_data(client, 316a5b79d62Sstigge@antcom.de MAX6639_REG_TARGTDUTY(attr->index), 317a5b79d62Sstigge@antcom.de data->pwm[attr->index]); 318a5b79d62Sstigge@antcom.de mutex_unlock(&data->update_lock); 319a5b79d62Sstigge@antcom.de return count; 320a5b79d62Sstigge@antcom.de } 321a5b79d62Sstigge@antcom.de 322*0a0ab22aSGuenter Roeck static ssize_t fan_input_show(struct device *dev, 323a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, char *buf) 324a5b79d62Sstigge@antcom.de { 325a5b79d62Sstigge@antcom.de struct max6639_data *data = max6639_update_device(dev); 326a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 327a5b79d62Sstigge@antcom.de 328a5b79d62Sstigge@antcom.de if (IS_ERR(data)) 329a5b79d62Sstigge@antcom.de return PTR_ERR(data); 330a5b79d62Sstigge@antcom.de 331a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index], 332b63d97a3SChris D Schimp data->rpm_range)); 333a5b79d62Sstigge@antcom.de } 334a5b79d62Sstigge@antcom.de 335*0a0ab22aSGuenter Roeck static ssize_t alarm_show(struct device *dev, 336a5b79d62Sstigge@antcom.de struct device_attribute *dev_attr, char *buf) 337a5b79d62Sstigge@antcom.de { 338a5b79d62Sstigge@antcom.de struct max6639_data *data = max6639_update_device(dev); 339a5b79d62Sstigge@antcom.de struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); 340a5b79d62Sstigge@antcom.de 341a5b79d62Sstigge@antcom.de if (IS_ERR(data)) 342a5b79d62Sstigge@antcom.de return PTR_ERR(data); 343a5b79d62Sstigge@antcom.de 344a5b79d62Sstigge@antcom.de return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index))); 345a5b79d62Sstigge@antcom.de } 346a5b79d62Sstigge@antcom.de 347*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0); 348*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1); 349*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0); 350*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1); 351*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0); 352*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1); 353*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0); 354*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1); 355*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0); 356*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1); 357*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); 358*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1); 359*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0); 360*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1); 361*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1); 362*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0); 363*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3); 364*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2); 365*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7); 366*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6); 367*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5); 368*0a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4); 369a5b79d62Sstigge@antcom.de 370a5b79d62Sstigge@antcom.de 3717981c584SGuenter Roeck static struct attribute *max6639_attrs[] = { 372a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_input.dev_attr.attr, 373a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_input.dev_attr.attr, 374a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_fault.dev_attr.attr, 375a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_fault.dev_attr.attr, 376a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_max.dev_attr.attr, 377a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_max.dev_attr.attr, 378a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_crit.dev_attr.attr, 379a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_crit.dev_attr.attr, 380a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_emergency.dev_attr.attr, 381a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_emergency.dev_attr.attr, 382a5b79d62Sstigge@antcom.de &sensor_dev_attr_pwm1.dev_attr.attr, 383a5b79d62Sstigge@antcom.de &sensor_dev_attr_pwm2.dev_attr.attr, 384a5b79d62Sstigge@antcom.de &sensor_dev_attr_fan1_input.dev_attr.attr, 385a5b79d62Sstigge@antcom.de &sensor_dev_attr_fan2_input.dev_attr.attr, 386a5b79d62Sstigge@antcom.de &sensor_dev_attr_fan1_fault.dev_attr.attr, 387a5b79d62Sstigge@antcom.de &sensor_dev_attr_fan2_fault.dev_attr.attr, 388a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, 389a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, 390a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, 391a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, 392a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr, 393a5b79d62Sstigge@antcom.de &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr, 394a5b79d62Sstigge@antcom.de NULL 395a5b79d62Sstigge@antcom.de }; 3967981c584SGuenter Roeck ATTRIBUTE_GROUPS(max6639); 397a5b79d62Sstigge@antcom.de 398a5b79d62Sstigge@antcom.de /* 399a5b79d62Sstigge@antcom.de * returns respective index in rpm_ranges table 400a5b79d62Sstigge@antcom.de * 1 by default on invalid range 401a5b79d62Sstigge@antcom.de */ 402a5b79d62Sstigge@antcom.de static int rpm_range_to_reg(int range) 403a5b79d62Sstigge@antcom.de { 404a5b79d62Sstigge@antcom.de int i; 405a5b79d62Sstigge@antcom.de 406a5b79d62Sstigge@antcom.de for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) { 407a5b79d62Sstigge@antcom.de if (rpm_ranges[i] == range) 408a5b79d62Sstigge@antcom.de return i; 409a5b79d62Sstigge@antcom.de } 410a5b79d62Sstigge@antcom.de 411a5b79d62Sstigge@antcom.de return 1; /* default: 4000 RPM */ 412a5b79d62Sstigge@antcom.de } 413a5b79d62Sstigge@antcom.de 4147981c584SGuenter Roeck static int max6639_init_client(struct i2c_client *client, 4157981c584SGuenter Roeck struct max6639_data *data) 416a5b79d62Sstigge@antcom.de { 417a5b79d62Sstigge@antcom.de struct max6639_platform_data *max6639_info = 418a8b3a3a5SJingoo Han dev_get_platdata(&client->dev); 4192f2da1acSChris D Schimp int i; 420a5b79d62Sstigge@antcom.de int rpm_range = 1; /* default: 4000 RPM */ 4212f2da1acSChris D Schimp int err; 422a5b79d62Sstigge@antcom.de 423177f3b92Sstigge@antcom.de /* Reset chip to default values, see below for GCONFIG setup */ 424a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, 425a5b79d62Sstigge@antcom.de MAX6639_GCONFIG_POR); 426a5b79d62Sstigge@antcom.de if (err) 427a5b79d62Sstigge@antcom.de goto exit; 428a5b79d62Sstigge@antcom.de 429a5b79d62Sstigge@antcom.de /* Fans pulse per revolution is 2 by default */ 430a5b79d62Sstigge@antcom.de if (max6639_info && max6639_info->ppr > 0 && 431a5b79d62Sstigge@antcom.de max6639_info->ppr < 5) 432a5b79d62Sstigge@antcom.de data->ppr = max6639_info->ppr; 433a5b79d62Sstigge@antcom.de else 434a5b79d62Sstigge@antcom.de data->ppr = 2; 435a5b79d62Sstigge@antcom.de data->ppr -= 1; 436a5b79d62Sstigge@antcom.de 437a5b79d62Sstigge@antcom.de if (max6639_info) 438a5b79d62Sstigge@antcom.de rpm_range = rpm_range_to_reg(max6639_info->rpm_range); 439a5b79d62Sstigge@antcom.de data->rpm_range = rpm_range; 440a5b79d62Sstigge@antcom.de 441a5b79d62Sstigge@antcom.de for (i = 0; i < 2; i++) { 442a5b79d62Sstigge@antcom.de 4432f2da1acSChris D Schimp /* Set Fan pulse per revolution */ 4442f2da1acSChris D Schimp err = i2c_smbus_write_byte_data(client, 4452f2da1acSChris D Schimp MAX6639_REG_FAN_PPR(i), 4462f2da1acSChris D Schimp data->ppr << 6); 4472f2da1acSChris D Schimp if (err) 4482f2da1acSChris D Schimp goto exit; 4492f2da1acSChris D Schimp 450a5b79d62Sstigge@antcom.de /* Fans config PWM, RPM */ 451a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 452a5b79d62Sstigge@antcom.de MAX6639_REG_FAN_CONFIG1(i), 453a5b79d62Sstigge@antcom.de MAX6639_FAN_CONFIG1_PWM | rpm_range); 454a5b79d62Sstigge@antcom.de if (err) 455a5b79d62Sstigge@antcom.de goto exit; 456a5b79d62Sstigge@antcom.de 457a5b79d62Sstigge@antcom.de /* Fans PWM polarity high by default */ 458a5b79d62Sstigge@antcom.de if (max6639_info && max6639_info->pwm_polarity == 0) 459a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 460a5b79d62Sstigge@antcom.de MAX6639_REG_FAN_CONFIG2a(i), 0x00); 461a5b79d62Sstigge@antcom.de else 462a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 463a5b79d62Sstigge@antcom.de MAX6639_REG_FAN_CONFIG2a(i), 0x02); 464a5b79d62Sstigge@antcom.de if (err) 465a5b79d62Sstigge@antcom.de goto exit; 466a5b79d62Sstigge@antcom.de 467177f3b92Sstigge@antcom.de /* 468177f3b92Sstigge@antcom.de * /THERM full speed enable, 469177f3b92Sstigge@antcom.de * PWM frequency 25kHz, see also GCONFIG below 470177f3b92Sstigge@antcom.de */ 471177f3b92Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 472177f3b92Sstigge@antcom.de MAX6639_REG_FAN_CONFIG3(i), 473177f3b92Sstigge@antcom.de MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03); 474177f3b92Sstigge@antcom.de if (err) 475177f3b92Sstigge@antcom.de goto exit; 476177f3b92Sstigge@antcom.de 477a5b79d62Sstigge@antcom.de /* Max. temp. 80C/90C/100C */ 478a5b79d62Sstigge@antcom.de data->temp_therm[i] = 80; 479a5b79d62Sstigge@antcom.de data->temp_alert[i] = 90; 480a5b79d62Sstigge@antcom.de data->temp_ot[i] = 100; 481a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 482a5b79d62Sstigge@antcom.de MAX6639_REG_THERM_LIMIT(i), 483a5b79d62Sstigge@antcom.de data->temp_therm[i]); 484a5b79d62Sstigge@antcom.de if (err) 485a5b79d62Sstigge@antcom.de goto exit; 486a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 487a5b79d62Sstigge@antcom.de MAX6639_REG_ALERT_LIMIT(i), 488a5b79d62Sstigge@antcom.de data->temp_alert[i]); 489a5b79d62Sstigge@antcom.de if (err) 490a5b79d62Sstigge@antcom.de goto exit; 491a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 492a5b79d62Sstigge@antcom.de MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]); 493a5b79d62Sstigge@antcom.de if (err) 494a5b79d62Sstigge@antcom.de goto exit; 495a5b79d62Sstigge@antcom.de 496a5b79d62Sstigge@antcom.de /* PWM 120/120 (i.e. 100%) */ 497a5b79d62Sstigge@antcom.de data->pwm[i] = 120; 498a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, 499a5b79d62Sstigge@antcom.de MAX6639_REG_TARGTDUTY(i), data->pwm[i]); 500a5b79d62Sstigge@antcom.de if (err) 501a5b79d62Sstigge@antcom.de goto exit; 502a5b79d62Sstigge@antcom.de } 503a5b79d62Sstigge@antcom.de /* Start monitoring */ 504a5b79d62Sstigge@antcom.de err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG, 505177f3b92Sstigge@antcom.de MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL | 506177f3b92Sstigge@antcom.de MAX6639_GCONFIG_PWM_FREQ_HI); 507a5b79d62Sstigge@antcom.de exit: 508a5b79d62Sstigge@antcom.de return err; 509a5b79d62Sstigge@antcom.de } 510a5b79d62Sstigge@antcom.de 511a5b79d62Sstigge@antcom.de /* Return 0 if detection is successful, -ENODEV otherwise */ 512a5b79d62Sstigge@antcom.de static int max6639_detect(struct i2c_client *client, 513a5b79d62Sstigge@antcom.de struct i2c_board_info *info) 514a5b79d62Sstigge@antcom.de { 515a5b79d62Sstigge@antcom.de struct i2c_adapter *adapter = client->adapter; 516a5b79d62Sstigge@antcom.de int dev_id, manu_id; 517a5b79d62Sstigge@antcom.de 518a5b79d62Sstigge@antcom.de if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 519a5b79d62Sstigge@antcom.de return -ENODEV; 520a5b79d62Sstigge@antcom.de 521a5b79d62Sstigge@antcom.de /* Actual detection via device and manufacturer ID */ 522a5b79d62Sstigge@antcom.de dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID); 523a5b79d62Sstigge@antcom.de manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID); 524a5b79d62Sstigge@antcom.de if (dev_id != 0x58 || manu_id != 0x4D) 525a5b79d62Sstigge@antcom.de return -ENODEV; 526a5b79d62Sstigge@antcom.de 527a5b79d62Sstigge@antcom.de strlcpy(info->type, "max6639", I2C_NAME_SIZE); 528a5b79d62Sstigge@antcom.de 529a5b79d62Sstigge@antcom.de return 0; 530a5b79d62Sstigge@antcom.de } 531a5b79d62Sstigge@antcom.de 532a5b79d62Sstigge@antcom.de static int max6639_probe(struct i2c_client *client, 533a5b79d62Sstigge@antcom.de const struct i2c_device_id *id) 534a5b79d62Sstigge@antcom.de { 535c1ea0a04SGuenter Roeck struct device *dev = &client->dev; 536a5b79d62Sstigge@antcom.de struct max6639_data *data; 5377981c584SGuenter Roeck struct device *hwmon_dev; 538a5b79d62Sstigge@antcom.de int err; 539a5b79d62Sstigge@antcom.de 540c1ea0a04SGuenter Roeck data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL); 541b07405fbSGuenter Roeck if (!data) 542b07405fbSGuenter Roeck return -ENOMEM; 543a5b79d62Sstigge@antcom.de 5447981c584SGuenter Roeck data->client = client; 545a5b79d62Sstigge@antcom.de mutex_init(&data->update_lock); 546a5b79d62Sstigge@antcom.de 547a5b79d62Sstigge@antcom.de /* Initialize the max6639 chip */ 5487981c584SGuenter Roeck err = max6639_init_client(client, data); 549a5b79d62Sstigge@antcom.de if (err < 0) 550b07405fbSGuenter Roeck return err; 551a5b79d62Sstigge@antcom.de 5527981c584SGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 5537981c584SGuenter Roeck data, 5547981c584SGuenter Roeck max6639_groups); 5557981c584SGuenter Roeck return PTR_ERR_OR_ZERO(hwmon_dev); 556a5b79d62Sstigge@antcom.de } 557a5b79d62Sstigge@antcom.de 55852f30f77SMark Brown #ifdef CONFIG_PM_SLEEP 55952f30f77SMark Brown static int max6639_suspend(struct device *dev) 560a5b79d62Sstigge@antcom.de { 56152f30f77SMark Brown struct i2c_client *client = to_i2c_client(dev); 562a5b79d62Sstigge@antcom.de int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); 563a5b79d62Sstigge@antcom.de if (data < 0) 564a5b79d62Sstigge@antcom.de return data; 565a5b79d62Sstigge@antcom.de 566a5b79d62Sstigge@antcom.de return i2c_smbus_write_byte_data(client, 567a5b79d62Sstigge@antcom.de MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY); 568a5b79d62Sstigge@antcom.de } 569a5b79d62Sstigge@antcom.de 57052f30f77SMark Brown static int max6639_resume(struct device *dev) 571a5b79d62Sstigge@antcom.de { 57252f30f77SMark Brown struct i2c_client *client = to_i2c_client(dev); 573a5b79d62Sstigge@antcom.de int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG); 574a5b79d62Sstigge@antcom.de if (data < 0) 575a5b79d62Sstigge@antcom.de return data; 576a5b79d62Sstigge@antcom.de 577a5b79d62Sstigge@antcom.de return i2c_smbus_write_byte_data(client, 578a5b79d62Sstigge@antcom.de MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY); 579a5b79d62Sstigge@antcom.de } 58052f30f77SMark Brown #endif /* CONFIG_PM_SLEEP */ 581a5b79d62Sstigge@antcom.de 582a5b79d62Sstigge@antcom.de static const struct i2c_device_id max6639_id[] = { 583a5b79d62Sstigge@antcom.de {"max6639", 0}, 584a5b79d62Sstigge@antcom.de { } 585a5b79d62Sstigge@antcom.de }; 586a5b79d62Sstigge@antcom.de 587a5b79d62Sstigge@antcom.de MODULE_DEVICE_TABLE(i2c, max6639_id); 588a5b79d62Sstigge@antcom.de 589768821a3SJingoo Han static SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume); 59052f30f77SMark Brown 591a5b79d62Sstigge@antcom.de static struct i2c_driver max6639_driver = { 592a5b79d62Sstigge@antcom.de .class = I2C_CLASS_HWMON, 593a5b79d62Sstigge@antcom.de .driver = { 594a5b79d62Sstigge@antcom.de .name = "max6639", 59552f30f77SMark Brown .pm = &max6639_pm_ops, 596a5b79d62Sstigge@antcom.de }, 597a5b79d62Sstigge@antcom.de .probe = max6639_probe, 598a5b79d62Sstigge@antcom.de .id_table = max6639_id, 599a5b79d62Sstigge@antcom.de .detect = max6639_detect, 600a5b79d62Sstigge@antcom.de .address_list = normal_i2c, 601a5b79d62Sstigge@antcom.de }; 602a5b79d62Sstigge@antcom.de 603f0967eeaSAxel Lin module_i2c_driver(max6639_driver); 604a5b79d62Sstigge@antcom.de 605a5b79d62Sstigge@antcom.de MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); 606a5b79d62Sstigge@antcom.de MODULE_DESCRIPTION("max6639 driver"); 607a5b79d62Sstigge@antcom.de MODULE_LICENSE("GPL"); 608