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