1aaf6fabfSChris Packham /* 2aaf6fabfSChris Packham * tc654.c - Linux kernel modules for fan speed controller 3aaf6fabfSChris Packham * 4aaf6fabfSChris Packham * Copyright (C) 2016 Allied Telesis Labs NZ 5aaf6fabfSChris Packham * 6aaf6fabfSChris Packham * This program is free software; you can redistribute it and/or modify 7aaf6fabfSChris Packham * it under the terms of the GNU General Public License as published by 8aaf6fabfSChris Packham * the Free Software Foundation; either version 2 of the License, or 9aaf6fabfSChris Packham * (at your option) any later version. 10aaf6fabfSChris Packham * 11aaf6fabfSChris Packham * This program is distributed in the hope that it will be useful, 12aaf6fabfSChris Packham * but WITHOUT ANY WARRANTY; without even the implied warranty of 13aaf6fabfSChris Packham * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14aaf6fabfSChris Packham * GNU General Public License for more details. 15aaf6fabfSChris Packham */ 16aaf6fabfSChris Packham 17aaf6fabfSChris Packham #include <linux/bitops.h> 18aaf6fabfSChris Packham #include <linux/err.h> 19aaf6fabfSChris Packham #include <linux/hwmon.h> 20aaf6fabfSChris Packham #include <linux/hwmon-sysfs.h> 21aaf6fabfSChris Packham #include <linux/i2c.h> 22aaf6fabfSChris Packham #include <linux/init.h> 23aaf6fabfSChris Packham #include <linux/jiffies.h> 24aaf6fabfSChris Packham #include <linux/module.h> 25aaf6fabfSChris Packham #include <linux/mutex.h> 26aaf6fabfSChris Packham #include <linux/slab.h> 27aaf6fabfSChris Packham #include <linux/util_macros.h> 28aaf6fabfSChris Packham 29aaf6fabfSChris Packham enum tc654_regs { 30aaf6fabfSChris Packham TC654_REG_RPM1 = 0x00, /* RPM Output 1 */ 31aaf6fabfSChris Packham TC654_REG_RPM2 = 0x01, /* RPM Output 2 */ 32aaf6fabfSChris Packham TC654_REG_FAN_FAULT1 = 0x02, /* Fan Fault 1 Threshold */ 33aaf6fabfSChris Packham TC654_REG_FAN_FAULT2 = 0x03, /* Fan Fault 2 Threshold */ 34aaf6fabfSChris Packham TC654_REG_CONFIG = 0x04, /* Configuration */ 35aaf6fabfSChris Packham TC654_REG_STATUS = 0x05, /* Status */ 36aaf6fabfSChris Packham TC654_REG_DUTY_CYCLE = 0x06, /* Fan Speed Duty Cycle */ 37aaf6fabfSChris Packham TC654_REG_MFR_ID = 0x07, /* Manufacturer Identification */ 38aaf6fabfSChris Packham TC654_REG_VER_ID = 0x08, /* Version Identification */ 39aaf6fabfSChris Packham }; 40aaf6fabfSChris Packham 41aaf6fabfSChris Packham /* Macros to easily index the registers */ 42aaf6fabfSChris Packham #define TC654_REG_RPM(idx) (TC654_REG_RPM1 + (idx)) 43aaf6fabfSChris Packham #define TC654_REG_FAN_FAULT(idx) (TC654_REG_FAN_FAULT1 + (idx)) 44aaf6fabfSChris Packham 45aaf6fabfSChris Packham /* Config register bits */ 46aaf6fabfSChris Packham #define TC654_REG_CONFIG_RES BIT(6) /* Resolution Selection */ 47aaf6fabfSChris Packham #define TC654_REG_CONFIG_DUTYC BIT(5) /* Duty Cycle Control */ 48aaf6fabfSChris Packham #define TC654_REG_CONFIG_SDM BIT(0) /* Shutdown Mode */ 49aaf6fabfSChris Packham 50aaf6fabfSChris Packham /* Status register bits */ 51aaf6fabfSChris Packham #define TC654_REG_STATUS_F2F BIT(1) /* Fan 2 Fault */ 52aaf6fabfSChris Packham #define TC654_REG_STATUS_F1F BIT(0) /* Fan 1 Fault */ 53aaf6fabfSChris Packham 54aaf6fabfSChris Packham /* RPM resolution for RPM Output registers */ 55aaf6fabfSChris Packham #define TC654_HIGH_RPM_RESOLUTION 25 /* 25 RPM resolution */ 56aaf6fabfSChris Packham #define TC654_LOW_RPM_RESOLUTION 50 /* 50 RPM resolution */ 57aaf6fabfSChris Packham 58aaf6fabfSChris Packham /* Convert to the fan fault RPM threshold from register value */ 59aaf6fabfSChris Packham #define TC654_FAN_FAULT_FROM_REG(val) ((val) * 50) /* 50 RPM resolution */ 60aaf6fabfSChris Packham 61aaf6fabfSChris Packham /* Convert to register value from the fan fault RPM threshold */ 62aaf6fabfSChris Packham #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff) 63aaf6fabfSChris Packham 64aaf6fabfSChris Packham /* Register data is read (and cached) at most once per second. */ 65aaf6fabfSChris Packham #define TC654_UPDATE_INTERVAL HZ 66aaf6fabfSChris Packham 67aaf6fabfSChris Packham struct tc654_data { 68aaf6fabfSChris Packham struct i2c_client *client; 69aaf6fabfSChris Packham 70aaf6fabfSChris Packham /* update mutex */ 71aaf6fabfSChris Packham struct mutex update_lock; 72aaf6fabfSChris Packham 73aaf6fabfSChris Packham /* tc654 register cache */ 74aaf6fabfSChris Packham bool valid; 75aaf6fabfSChris Packham unsigned long last_updated; /* in jiffies */ 76aaf6fabfSChris Packham 77aaf6fabfSChris Packham u8 rpm_output[2]; /* The fan RPM data for fans 1 and 2 is then 78aaf6fabfSChris Packham * written to registers RPM1 and RPM2 79aaf6fabfSChris Packham */ 80aaf6fabfSChris Packham u8 fan_fault[2]; /* The Fan Fault Threshold Registers are used to 81aaf6fabfSChris Packham * set the fan fault threshold levels for fan 1 82aaf6fabfSChris Packham * and fan 2 83aaf6fabfSChris Packham */ 84aaf6fabfSChris Packham u8 config; /* The Configuration Register is an 8-bit read/ 85aaf6fabfSChris Packham * writable multi-function control register 86aaf6fabfSChris Packham * 7: Fan Fault Clear 87aaf6fabfSChris Packham * 1 = Clear Fan Fault 88aaf6fabfSChris Packham * 0 = Normal Operation (default) 89aaf6fabfSChris Packham * 6: Resolution Selection for RPM Output Registers 90aaf6fabfSChris Packham * RPM Output Registers (RPM1 and RPM2) will be 91aaf6fabfSChris Packham * set for 92aaf6fabfSChris Packham * 1 = 25 RPM (9-bit) resolution 93aaf6fabfSChris Packham * 0 = 50 RPM (8-bit) resolution (default) 94aaf6fabfSChris Packham * 5: Duty Cycle Control Method 95aaf6fabfSChris Packham * The V OUT duty cycle will be controlled via 96aaf6fabfSChris Packham * 1 = the SMBus interface. 97aaf6fabfSChris Packham * 0 = via the V IN analog input pin. (default) 98aaf6fabfSChris Packham * 4,3: Fan 2 Pulses Per Rotation 99aaf6fabfSChris Packham * 00 = 1 100aaf6fabfSChris Packham * 01 = 2 (default) 101aaf6fabfSChris Packham * 10 = 4 102aaf6fabfSChris Packham * 11 = 8 103aaf6fabfSChris Packham * 2,1: Fan 1 Pulses Per Rotation 104aaf6fabfSChris Packham * 00 = 1 105aaf6fabfSChris Packham * 01 = 2 (default) 106aaf6fabfSChris Packham * 10 = 4 107aaf6fabfSChris Packham * 11 = 8 108aaf6fabfSChris Packham * 0: Shutdown Mode 109aaf6fabfSChris Packham * 1 = Shutdown mode. 110aaf6fabfSChris Packham * 0 = Normal operation. (default) 111aaf6fabfSChris Packham */ 112aaf6fabfSChris Packham u8 status; /* The Status register provides all the information 113aaf6fabfSChris Packham * about what is going on within the TC654/TC655 114aaf6fabfSChris Packham * devices. 115aaf6fabfSChris Packham * 7,6: Unimplemented, Read as '0' 116aaf6fabfSChris Packham * 5: Over-Temperature Fault Condition 117aaf6fabfSChris Packham * 1 = Over-Temperature condition has occurred 118aaf6fabfSChris Packham * 0 = Normal operation. V IN is less than 2.6V 119aaf6fabfSChris Packham * 4: RPM2 Counter Overflow 120aaf6fabfSChris Packham * 1 = Fault condition 121aaf6fabfSChris Packham * 0 = Normal operation 122aaf6fabfSChris Packham * 3: RPM1 Counter Overflow 123aaf6fabfSChris Packham * 1 = Fault condition 124aaf6fabfSChris Packham * 0 = Normal operation 125aaf6fabfSChris Packham * 2: V IN Input Status 126aaf6fabfSChris Packham * 1 = V IN is open 127aaf6fabfSChris Packham * 0 = Normal operation. voltage present at V IN 128aaf6fabfSChris Packham * 1: Fan 2 Fault 129aaf6fabfSChris Packham * 1 = Fault condition 130aaf6fabfSChris Packham * 0 = Normal operation 131aaf6fabfSChris Packham * 0: Fan 1 Fault 132aaf6fabfSChris Packham * 1 = Fault condition 133aaf6fabfSChris Packham * 0 = Normal operation 134aaf6fabfSChris Packham */ 135aaf6fabfSChris Packham u8 duty_cycle; /* The DUTY_CYCLE register is a 4-bit read/ 136aaf6fabfSChris Packham * writable register used to control the duty 137aaf6fabfSChris Packham * cycle of the V OUT output. 138aaf6fabfSChris Packham */ 139aaf6fabfSChris Packham }; 140aaf6fabfSChris Packham 141aaf6fabfSChris Packham /* helper to grab and cache data, at most one time per second */ 142aaf6fabfSChris Packham static struct tc654_data *tc654_update_client(struct device *dev) 143aaf6fabfSChris Packham { 144aaf6fabfSChris Packham struct tc654_data *data = dev_get_drvdata(dev); 145aaf6fabfSChris Packham struct i2c_client *client = data->client; 146aaf6fabfSChris Packham int ret = 0; 147aaf6fabfSChris Packham 148aaf6fabfSChris Packham mutex_lock(&data->update_lock); 149aaf6fabfSChris Packham if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) && 150aaf6fabfSChris Packham likely(data->valid)) 151aaf6fabfSChris Packham goto out; 152aaf6fabfSChris Packham 153aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0)); 154aaf6fabfSChris Packham if (ret < 0) 155aaf6fabfSChris Packham goto out; 156aaf6fabfSChris Packham data->rpm_output[0] = ret; 157aaf6fabfSChris Packham 158aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1)); 159aaf6fabfSChris Packham if (ret < 0) 160aaf6fabfSChris Packham goto out; 161aaf6fabfSChris Packham data->rpm_output[1] = ret; 162aaf6fabfSChris Packham 163aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0)); 164aaf6fabfSChris Packham if (ret < 0) 165aaf6fabfSChris Packham goto out; 166aaf6fabfSChris Packham data->fan_fault[0] = ret; 167aaf6fabfSChris Packham 168aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1)); 169aaf6fabfSChris Packham if (ret < 0) 170aaf6fabfSChris Packham goto out; 171aaf6fabfSChris Packham data->fan_fault[1] = ret; 172aaf6fabfSChris Packham 173aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG); 174aaf6fabfSChris Packham if (ret < 0) 175aaf6fabfSChris Packham goto out; 176aaf6fabfSChris Packham data->config = ret; 177aaf6fabfSChris Packham 178aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS); 179aaf6fabfSChris Packham if (ret < 0) 180aaf6fabfSChris Packham goto out; 181aaf6fabfSChris Packham data->status = ret; 182aaf6fabfSChris Packham 183aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE); 184aaf6fabfSChris Packham if (ret < 0) 185aaf6fabfSChris Packham goto out; 186aaf6fabfSChris Packham data->duty_cycle = ret & 0x0f; 187aaf6fabfSChris Packham 188aaf6fabfSChris Packham data->last_updated = jiffies; 189aaf6fabfSChris Packham data->valid = true; 190aaf6fabfSChris Packham out: 191aaf6fabfSChris Packham mutex_unlock(&data->update_lock); 192aaf6fabfSChris Packham 193aaf6fabfSChris Packham if (ret < 0) /* upon error, encode it in return value */ 194aaf6fabfSChris Packham data = ERR_PTR(ret); 195aaf6fabfSChris Packham 196aaf6fabfSChris Packham return data; 197aaf6fabfSChris Packham } 198aaf6fabfSChris Packham 199aaf6fabfSChris Packham /* 200aaf6fabfSChris Packham * sysfs attributes 201aaf6fabfSChris Packham */ 202aaf6fabfSChris Packham 203*1acd2e29SGuenter Roeck static ssize_t fan_show(struct device *dev, struct device_attribute *da, 204aaf6fabfSChris Packham char *buf) 205aaf6fabfSChris Packham { 206aaf6fabfSChris Packham int nr = to_sensor_dev_attr(da)->index; 207aaf6fabfSChris Packham struct tc654_data *data = tc654_update_client(dev); 208aaf6fabfSChris Packham int val; 209aaf6fabfSChris Packham 210aaf6fabfSChris Packham if (IS_ERR(data)) 211aaf6fabfSChris Packham return PTR_ERR(data); 212aaf6fabfSChris Packham 213aaf6fabfSChris Packham if (data->config & TC654_REG_CONFIG_RES) 214aaf6fabfSChris Packham val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION; 215aaf6fabfSChris Packham else 216aaf6fabfSChris Packham val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION; 217aaf6fabfSChris Packham 218aaf6fabfSChris Packham return sprintf(buf, "%d\n", val); 219aaf6fabfSChris Packham } 220aaf6fabfSChris Packham 221*1acd2e29SGuenter Roeck static ssize_t fan_min_show(struct device *dev, struct device_attribute *da, 222aaf6fabfSChris Packham char *buf) 223aaf6fabfSChris Packham { 224aaf6fabfSChris Packham int nr = to_sensor_dev_attr(da)->index; 225aaf6fabfSChris Packham struct tc654_data *data = tc654_update_client(dev); 226aaf6fabfSChris Packham 227aaf6fabfSChris Packham if (IS_ERR(data)) 228aaf6fabfSChris Packham return PTR_ERR(data); 229aaf6fabfSChris Packham 230aaf6fabfSChris Packham return sprintf(buf, "%d\n", 231aaf6fabfSChris Packham TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr])); 232aaf6fabfSChris Packham } 233aaf6fabfSChris Packham 234*1acd2e29SGuenter Roeck static ssize_t fan_min_store(struct device *dev, struct device_attribute *da, 235aaf6fabfSChris Packham const char *buf, size_t count) 236aaf6fabfSChris Packham { 237aaf6fabfSChris Packham int nr = to_sensor_dev_attr(da)->index; 238aaf6fabfSChris Packham struct tc654_data *data = dev_get_drvdata(dev); 239aaf6fabfSChris Packham struct i2c_client *client = data->client; 240aaf6fabfSChris Packham unsigned long val; 241aaf6fabfSChris Packham int ret; 242aaf6fabfSChris Packham 243aaf6fabfSChris Packham if (kstrtoul(buf, 10, &val)) 244aaf6fabfSChris Packham return -EINVAL; 245aaf6fabfSChris Packham 246aaf6fabfSChris Packham val = clamp_val(val, 0, 12750); 247aaf6fabfSChris Packham 248aaf6fabfSChris Packham mutex_lock(&data->update_lock); 249aaf6fabfSChris Packham 250aaf6fabfSChris Packham data->fan_fault[nr] = TC654_FAN_FAULT_TO_REG(val); 251aaf6fabfSChris Packham ret = i2c_smbus_write_byte_data(client, TC654_REG_FAN_FAULT(nr), 252aaf6fabfSChris Packham data->fan_fault[nr]); 253aaf6fabfSChris Packham 254aaf6fabfSChris Packham mutex_unlock(&data->update_lock); 255aaf6fabfSChris Packham return ret < 0 ? ret : count; 256aaf6fabfSChris Packham } 257aaf6fabfSChris Packham 258*1acd2e29SGuenter Roeck static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da, 259aaf6fabfSChris Packham char *buf) 260aaf6fabfSChris Packham { 261aaf6fabfSChris Packham int nr = to_sensor_dev_attr(da)->index; 262aaf6fabfSChris Packham struct tc654_data *data = tc654_update_client(dev); 263aaf6fabfSChris Packham int val; 264aaf6fabfSChris Packham 265aaf6fabfSChris Packham if (IS_ERR(data)) 266aaf6fabfSChris Packham return PTR_ERR(data); 267aaf6fabfSChris Packham 268aaf6fabfSChris Packham if (nr == 0) 269aaf6fabfSChris Packham val = !!(data->status & TC654_REG_STATUS_F1F); 270aaf6fabfSChris Packham else 271aaf6fabfSChris Packham val = !!(data->status & TC654_REG_STATUS_F2F); 272aaf6fabfSChris Packham 273aaf6fabfSChris Packham return sprintf(buf, "%d\n", val); 274aaf6fabfSChris Packham } 275aaf6fabfSChris Packham 276aaf6fabfSChris Packham static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 }; 277aaf6fabfSChris Packham 278*1acd2e29SGuenter Roeck static ssize_t fan_pulses_show(struct device *dev, 279*1acd2e29SGuenter Roeck struct device_attribute *da, char *buf) 280aaf6fabfSChris Packham { 281aaf6fabfSChris Packham int nr = to_sensor_dev_attr(da)->index; 282aaf6fabfSChris Packham struct tc654_data *data = tc654_update_client(dev); 283aaf6fabfSChris Packham u8 val; 284aaf6fabfSChris Packham 285aaf6fabfSChris Packham if (IS_ERR(data)) 286aaf6fabfSChris Packham return PTR_ERR(data); 287aaf6fabfSChris Packham 288aaf6fabfSChris Packham val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03); 289aaf6fabfSChris Packham return sprintf(buf, "%d\n", val); 290aaf6fabfSChris Packham } 291aaf6fabfSChris Packham 292*1acd2e29SGuenter Roeck static ssize_t fan_pulses_store(struct device *dev, 293*1acd2e29SGuenter Roeck struct device_attribute *da, const char *buf, 294*1acd2e29SGuenter Roeck size_t count) 295aaf6fabfSChris Packham { 296aaf6fabfSChris Packham int nr = to_sensor_dev_attr(da)->index; 297aaf6fabfSChris Packham struct tc654_data *data = dev_get_drvdata(dev); 298aaf6fabfSChris Packham struct i2c_client *client = data->client; 299aaf6fabfSChris Packham u8 config; 300aaf6fabfSChris Packham unsigned long val; 301aaf6fabfSChris Packham int ret; 302aaf6fabfSChris Packham 303aaf6fabfSChris Packham if (kstrtoul(buf, 10, &val)) 304aaf6fabfSChris Packham return -EINVAL; 305aaf6fabfSChris Packham 306aaf6fabfSChris Packham switch (val) { 307aaf6fabfSChris Packham case 1: 308aaf6fabfSChris Packham config = 0; 309aaf6fabfSChris Packham break; 310aaf6fabfSChris Packham case 2: 311aaf6fabfSChris Packham config = 1; 312aaf6fabfSChris Packham break; 313aaf6fabfSChris Packham case 4: 314aaf6fabfSChris Packham config = 2; 315aaf6fabfSChris Packham break; 316aaf6fabfSChris Packham case 8: 317aaf6fabfSChris Packham config = 3; 318aaf6fabfSChris Packham break; 319aaf6fabfSChris Packham default: 320aaf6fabfSChris Packham return -EINVAL; 321aaf6fabfSChris Packham } 322aaf6fabfSChris Packham 323aaf6fabfSChris Packham mutex_lock(&data->update_lock); 324aaf6fabfSChris Packham 325aaf6fabfSChris Packham data->config &= ~(0x03 << TC654_FAN_PULSE_SHIFT[nr]); 326aaf6fabfSChris Packham data->config |= (config << TC654_FAN_PULSE_SHIFT[nr]); 327aaf6fabfSChris Packham ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config); 328aaf6fabfSChris Packham 329aaf6fabfSChris Packham mutex_unlock(&data->update_lock); 330aaf6fabfSChris Packham return ret < 0 ? ret : count; 331aaf6fabfSChris Packham } 332aaf6fabfSChris Packham 333*1acd2e29SGuenter Roeck static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da, 334*1acd2e29SGuenter Roeck char *buf) 335aaf6fabfSChris Packham { 336aaf6fabfSChris Packham struct tc654_data *data = tc654_update_client(dev); 337aaf6fabfSChris Packham 338aaf6fabfSChris Packham if (IS_ERR(data)) 339aaf6fabfSChris Packham return PTR_ERR(data); 340aaf6fabfSChris Packham 341aaf6fabfSChris Packham return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC)); 342aaf6fabfSChris Packham } 343aaf6fabfSChris Packham 344*1acd2e29SGuenter Roeck static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da, 345aaf6fabfSChris Packham const char *buf, size_t count) 346aaf6fabfSChris Packham { 347aaf6fabfSChris Packham struct tc654_data *data = dev_get_drvdata(dev); 348aaf6fabfSChris Packham struct i2c_client *client = data->client; 349aaf6fabfSChris Packham unsigned long val; 350aaf6fabfSChris Packham int ret; 351aaf6fabfSChris Packham 352aaf6fabfSChris Packham if (kstrtoul(buf, 10, &val)) 353aaf6fabfSChris Packham return -EINVAL; 354aaf6fabfSChris Packham 355aaf6fabfSChris Packham if (val != 0 && val != 1) 356aaf6fabfSChris Packham return -EINVAL; 357aaf6fabfSChris Packham 358aaf6fabfSChris Packham mutex_lock(&data->update_lock); 359aaf6fabfSChris Packham 360aaf6fabfSChris Packham if (val) 361aaf6fabfSChris Packham data->config |= TC654_REG_CONFIG_DUTYC; 362aaf6fabfSChris Packham else 363aaf6fabfSChris Packham data->config &= ~TC654_REG_CONFIG_DUTYC; 364aaf6fabfSChris Packham 365aaf6fabfSChris Packham ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config); 366aaf6fabfSChris Packham 367aaf6fabfSChris Packham mutex_unlock(&data->update_lock); 368aaf6fabfSChris Packham return ret < 0 ? ret : count; 369aaf6fabfSChris Packham } 370aaf6fabfSChris Packham 371aaf6fabfSChris Packham static const int tc654_pwm_map[16] = { 77, 88, 102, 112, 124, 136, 148, 160, 372aaf6fabfSChris Packham 172, 184, 196, 207, 219, 231, 243, 255}; 373aaf6fabfSChris Packham 374*1acd2e29SGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *da, 375aaf6fabfSChris Packham char *buf) 376aaf6fabfSChris Packham { 377aaf6fabfSChris Packham struct tc654_data *data = tc654_update_client(dev); 378aaf6fabfSChris Packham int pwm; 379aaf6fabfSChris Packham 380aaf6fabfSChris Packham if (IS_ERR(data)) 381aaf6fabfSChris Packham return PTR_ERR(data); 382aaf6fabfSChris Packham 383aaf6fabfSChris Packham if (data->config & TC654_REG_CONFIG_SDM) 384aaf6fabfSChris Packham pwm = 0; 385aaf6fabfSChris Packham else 386aaf6fabfSChris Packham pwm = tc654_pwm_map[data->duty_cycle]; 387aaf6fabfSChris Packham 388aaf6fabfSChris Packham return sprintf(buf, "%d\n", pwm); 389aaf6fabfSChris Packham } 390aaf6fabfSChris Packham 391*1acd2e29SGuenter Roeck static ssize_t pwm_store(struct device *dev, struct device_attribute *da, 392aaf6fabfSChris Packham const char *buf, size_t count) 393aaf6fabfSChris Packham { 394aaf6fabfSChris Packham struct tc654_data *data = dev_get_drvdata(dev); 395aaf6fabfSChris Packham struct i2c_client *client = data->client; 396aaf6fabfSChris Packham unsigned long val; 397aaf6fabfSChris Packham int ret; 398aaf6fabfSChris Packham 399aaf6fabfSChris Packham if (kstrtoul(buf, 10, &val)) 400aaf6fabfSChris Packham return -EINVAL; 401aaf6fabfSChris Packham if (val > 255) 402aaf6fabfSChris Packham return -EINVAL; 403aaf6fabfSChris Packham 404aaf6fabfSChris Packham mutex_lock(&data->update_lock); 405aaf6fabfSChris Packham 406aaf6fabfSChris Packham if (val == 0) 407aaf6fabfSChris Packham data->config |= TC654_REG_CONFIG_SDM; 408aaf6fabfSChris Packham else 409aaf6fabfSChris Packham data->config &= ~TC654_REG_CONFIG_SDM; 410aaf6fabfSChris Packham 411aaf6fabfSChris Packham data->duty_cycle = find_closest(val, tc654_pwm_map, 412aaf6fabfSChris Packham ARRAY_SIZE(tc654_pwm_map)); 413aaf6fabfSChris Packham 414aaf6fabfSChris Packham ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config); 415aaf6fabfSChris Packham if (ret < 0) 416aaf6fabfSChris Packham goto out; 417aaf6fabfSChris Packham 418aaf6fabfSChris Packham ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE, 419aaf6fabfSChris Packham data->duty_cycle); 420aaf6fabfSChris Packham 421aaf6fabfSChris Packham out: 422aaf6fabfSChris Packham mutex_unlock(&data->update_lock); 423aaf6fabfSChris Packham return ret < 0 ? ret : count; 424aaf6fabfSChris Packham } 425aaf6fabfSChris Packham 426*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0); 427*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1); 428*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0); 429*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1); 430*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0); 431*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1); 432*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0); 433*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1); 434*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0); 435*1acd2e29SGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0); 436aaf6fabfSChris Packham 437aaf6fabfSChris Packham /* Driver data */ 438aaf6fabfSChris Packham static struct attribute *tc654_attrs[] = { 439aaf6fabfSChris Packham &sensor_dev_attr_fan1_input.dev_attr.attr, 440aaf6fabfSChris Packham &sensor_dev_attr_fan2_input.dev_attr.attr, 441aaf6fabfSChris Packham &sensor_dev_attr_fan1_min.dev_attr.attr, 442aaf6fabfSChris Packham &sensor_dev_attr_fan2_min.dev_attr.attr, 443aaf6fabfSChris Packham &sensor_dev_attr_fan1_alarm.dev_attr.attr, 444aaf6fabfSChris Packham &sensor_dev_attr_fan2_alarm.dev_attr.attr, 445aaf6fabfSChris Packham &sensor_dev_attr_fan1_pulses.dev_attr.attr, 446aaf6fabfSChris Packham &sensor_dev_attr_fan2_pulses.dev_attr.attr, 447aaf6fabfSChris Packham &sensor_dev_attr_pwm1_mode.dev_attr.attr, 448aaf6fabfSChris Packham &sensor_dev_attr_pwm1.dev_attr.attr, 449aaf6fabfSChris Packham NULL 450aaf6fabfSChris Packham }; 451aaf6fabfSChris Packham 452aaf6fabfSChris Packham ATTRIBUTE_GROUPS(tc654); 453aaf6fabfSChris Packham 454aaf6fabfSChris Packham /* 455aaf6fabfSChris Packham * device probe and removal 456aaf6fabfSChris Packham */ 457aaf6fabfSChris Packham 458aaf6fabfSChris Packham static int tc654_probe(struct i2c_client *client, 459aaf6fabfSChris Packham const struct i2c_device_id *id) 460aaf6fabfSChris Packham { 461aaf6fabfSChris Packham struct device *dev = &client->dev; 462aaf6fabfSChris Packham struct tc654_data *data; 463aaf6fabfSChris Packham struct device *hwmon_dev; 464aaf6fabfSChris Packham int ret; 465aaf6fabfSChris Packham 466aaf6fabfSChris Packham if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 467aaf6fabfSChris Packham return -ENODEV; 468aaf6fabfSChris Packham 469aaf6fabfSChris Packham data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL); 470aaf6fabfSChris Packham if (!data) 471aaf6fabfSChris Packham return -ENOMEM; 472aaf6fabfSChris Packham 473aaf6fabfSChris Packham data->client = client; 474aaf6fabfSChris Packham mutex_init(&data->update_lock); 475aaf6fabfSChris Packham 476aaf6fabfSChris Packham ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG); 477aaf6fabfSChris Packham if (ret < 0) 478aaf6fabfSChris Packham return ret; 479aaf6fabfSChris Packham 480aaf6fabfSChris Packham data->config = ret; 481aaf6fabfSChris Packham 482aaf6fabfSChris Packham hwmon_dev = 483aaf6fabfSChris Packham devm_hwmon_device_register_with_groups(dev, client->name, data, 484aaf6fabfSChris Packham tc654_groups); 485aaf6fabfSChris Packham return PTR_ERR_OR_ZERO(hwmon_dev); 486aaf6fabfSChris Packham } 487aaf6fabfSChris Packham 488aaf6fabfSChris Packham static const struct i2c_device_id tc654_id[] = { 489aaf6fabfSChris Packham {"tc654", 0}, 490aaf6fabfSChris Packham {"tc655", 0}, 491aaf6fabfSChris Packham {} 492aaf6fabfSChris Packham }; 493aaf6fabfSChris Packham 494aaf6fabfSChris Packham MODULE_DEVICE_TABLE(i2c, tc654_id); 495aaf6fabfSChris Packham 496aaf6fabfSChris Packham static struct i2c_driver tc654_driver = { 497aaf6fabfSChris Packham .driver = { 498aaf6fabfSChris Packham .name = "tc654", 499aaf6fabfSChris Packham }, 500aaf6fabfSChris Packham .probe = tc654_probe, 501aaf6fabfSChris Packham .id_table = tc654_id, 502aaf6fabfSChris Packham }; 503aaf6fabfSChris Packham 504aaf6fabfSChris Packham module_i2c_driver(tc654_driver); 505aaf6fabfSChris Packham 506aaf6fabfSChris Packham MODULE_AUTHOR("Allied Telesis Labs"); 507aaf6fabfSChris Packham MODULE_DESCRIPTION("Microchip TC654/TC655 driver"); 508aaf6fabfSChris Packham MODULE_LICENSE("GPL"); 509