1594fbe71SArnaud Ebalard /* 2594fbe71SArnaud Ebalard * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed 3594fbe71SArnaud Ebalard * PWM controller chips from G762 family, i.e. G762 and G763 4594fbe71SArnaud Ebalard * 5594fbe71SArnaud Ebalard * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org> 6594fbe71SArnaud Ebalard * 7594fbe71SArnaud Ebalard * This work is based on a basic version for 2.6.31 kernel developed 8594fbe71SArnaud Ebalard * by Olivier Mouchet for LaCie. Updates and correction have been 9594fbe71SArnaud Ebalard * performed to run on recent kernels. Additional features, like the 10594fbe71SArnaud Ebalard * ability to configure various characteristics via .dts file or 11594fbe71SArnaud Ebalard * board init file have been added. Detailed datasheet on which this 12594fbe71SArnaud Ebalard * development is based is available here: 13594fbe71SArnaud Ebalard * 14594fbe71SArnaud Ebalard * http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf 15594fbe71SArnaud Ebalard * 16594fbe71SArnaud Ebalard * Headers from previous developments have been kept below: 17594fbe71SArnaud Ebalard * 18594fbe71SArnaud Ebalard * Copyright (c) 2009 LaCie 19594fbe71SArnaud Ebalard * 20594fbe71SArnaud Ebalard * Author: Olivier Mouchet <olivier.mouchet@gmail.com> 21594fbe71SArnaud Ebalard * 22594fbe71SArnaud Ebalard * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org> 23594fbe71SArnaud Ebalard * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org> 24594fbe71SArnaud Ebalard * 25594fbe71SArnaud Ebalard * g762: minimal datasheet available at: 26594fbe71SArnaud Ebalard * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf 27594fbe71SArnaud Ebalard * 28594fbe71SArnaud Ebalard * This program is free software; you can redistribute it and/or modify 29594fbe71SArnaud Ebalard * it under the terms of the GNU General Public License as published by 30594fbe71SArnaud Ebalard * the Free Software Foundation; either version 2 of the License, or 31594fbe71SArnaud Ebalard * (at your option) any later version. 32594fbe71SArnaud Ebalard * 33594fbe71SArnaud Ebalard * This program is distributed in the hope that it will be useful, 34594fbe71SArnaud Ebalard * but WITHOUT ANY WARRANTY; without even the implied warranty of 35594fbe71SArnaud Ebalard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 36594fbe71SArnaud Ebalard * GNU General Public License for more details. 37594fbe71SArnaud Ebalard * 38594fbe71SArnaud Ebalard * You should have received a copy of the GNU General Public License 39594fbe71SArnaud Ebalard * along with this program; if not, write to the Free Software 40594fbe71SArnaud Ebalard * Foundation. 41594fbe71SArnaud Ebalard */ 42594fbe71SArnaud Ebalard 43594fbe71SArnaud Ebalard #include <linux/device.h> 44594fbe71SArnaud Ebalard #include <linux/module.h> 45594fbe71SArnaud Ebalard #include <linux/init.h> 46594fbe71SArnaud Ebalard #include <linux/jiffies.h> 47594fbe71SArnaud Ebalard #include <linux/i2c.h> 48594fbe71SArnaud Ebalard #include <linux/hwmon.h> 49594fbe71SArnaud Ebalard #include <linux/hwmon-sysfs.h> 50594fbe71SArnaud Ebalard #include <linux/err.h> 51594fbe71SArnaud Ebalard #include <linux/mutex.h> 52594fbe71SArnaud Ebalard #include <linux/kernel.h> 53594fbe71SArnaud Ebalard #include <linux/clk.h> 54594fbe71SArnaud Ebalard #include <linux/of.h> 55594fbe71SArnaud Ebalard #include <linux/of_device.h> 56594fbe71SArnaud Ebalard #include <linux/platform_data/g762.h> 57594fbe71SArnaud Ebalard 58594fbe71SArnaud Ebalard #define DRVNAME "g762" 59594fbe71SArnaud Ebalard 60594fbe71SArnaud Ebalard static const struct i2c_device_id g762_id[] = { 61594fbe71SArnaud Ebalard { "g762", 0 }, 62594fbe71SArnaud Ebalard { "g763", 0 }, 63594fbe71SArnaud Ebalard { } 64594fbe71SArnaud Ebalard }; 65594fbe71SArnaud Ebalard MODULE_DEVICE_TABLE(i2c, g762_id); 66594fbe71SArnaud Ebalard 67594fbe71SArnaud Ebalard enum g762_regs { 68594fbe71SArnaud Ebalard G762_REG_SET_CNT = 0x00, 69594fbe71SArnaud Ebalard G762_REG_ACT_CNT = 0x01, 70594fbe71SArnaud Ebalard G762_REG_FAN_STA = 0x02, 71594fbe71SArnaud Ebalard G762_REG_SET_OUT = 0x03, 72594fbe71SArnaud Ebalard G762_REG_FAN_CMD1 = 0x04, 73594fbe71SArnaud Ebalard G762_REG_FAN_CMD2 = 0x05, 74594fbe71SArnaud Ebalard }; 75594fbe71SArnaud Ebalard 76594fbe71SArnaud Ebalard /* Config register bits */ 77594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */ 78594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */ 79594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */ 80594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */ 81594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */ 82594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04 83594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */ 84594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */ 85594fbe71SArnaud Ebalard 86594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */ 87594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04 88594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */ 89594fbe71SArnaud Ebalard #define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01 90594fbe71SArnaud Ebalard 91594fbe71SArnaud Ebalard #define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */ 92594fbe71SArnaud Ebalard #define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */ 93594fbe71SArnaud Ebalard 94594fbe71SArnaud Ebalard /* Config register values */ 95594fbe71SArnaud Ebalard #define G762_OUT_MODE_PWM 1 96594fbe71SArnaud Ebalard #define G762_OUT_MODE_DC 0 97594fbe71SArnaud Ebalard 98594fbe71SArnaud Ebalard #define G762_FAN_MODE_CLOSED_LOOP 2 99594fbe71SArnaud Ebalard #define G762_FAN_MODE_OPEN_LOOP 1 100594fbe71SArnaud Ebalard 101594fbe71SArnaud Ebalard #define G762_PWM_POLARITY_NEGATIVE 1 102594fbe71SArnaud Ebalard #define G762_PWM_POLARITY_POSITIVE 0 103594fbe71SArnaud Ebalard 104594fbe71SArnaud Ebalard /* Register data is read (and cached) at most once per second. */ 105594fbe71SArnaud Ebalard #define G762_UPDATE_INTERVAL HZ 106594fbe71SArnaud Ebalard 107594fbe71SArnaud Ebalard /* 108594fbe71SArnaud Ebalard * Extract pulse count per fan revolution value (2 or 4) from given 109594fbe71SArnaud Ebalard * FAN_CMD1 register value. 110594fbe71SArnaud Ebalard */ 111594fbe71SArnaud Ebalard #define G762_PULSE_FROM_REG(reg) \ 112594fbe71SArnaud Ebalard ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1) 113594fbe71SArnaud Ebalard 114594fbe71SArnaud Ebalard /* 115594fbe71SArnaud Ebalard * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1 116594fbe71SArnaud Ebalard * register value. 117594fbe71SArnaud Ebalard */ 118594fbe71SArnaud Ebalard #define G762_CLKDIV_FROM_REG(reg) \ 119594fbe71SArnaud Ebalard (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \ 120594fbe71SArnaud Ebalard G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2)) 121594fbe71SArnaud Ebalard 122594fbe71SArnaud Ebalard /* 123594fbe71SArnaud Ebalard * Extract fan gear mode multiplier value (0, 2 or 4) from given 124594fbe71SArnaud Ebalard * FAN_CMD2 register value. 125594fbe71SArnaud Ebalard */ 126594fbe71SArnaud Ebalard #define G762_GEARMULT_FROM_REG(reg) \ 127594fbe71SArnaud Ebalard (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \ 128594fbe71SArnaud Ebalard G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2)) 129594fbe71SArnaud Ebalard 130594fbe71SArnaud Ebalard struct g762_data { 131594fbe71SArnaud Ebalard struct device *hwmon_dev; 132398e16dbSAxel Lin struct i2c_client *client; 133594fbe71SArnaud Ebalard struct clk *clk; 134594fbe71SArnaud Ebalard 135594fbe71SArnaud Ebalard /* update mutex */ 136594fbe71SArnaud Ebalard struct mutex update_lock; 137594fbe71SArnaud Ebalard 138594fbe71SArnaud Ebalard /* board specific parameters. */ 139594fbe71SArnaud Ebalard u32 clk_freq; 140594fbe71SArnaud Ebalard 141594fbe71SArnaud Ebalard /* g762 register cache */ 142594fbe71SArnaud Ebalard bool valid; 143594fbe71SArnaud Ebalard unsigned long last_updated; /* in jiffies */ 144594fbe71SArnaud Ebalard 145594fbe71SArnaud Ebalard u8 set_cnt; /* controls fan rotation speed in closed-loop mode */ 146594fbe71SArnaud Ebalard u8 act_cnt; /* provides access to current fan RPM value */ 147594fbe71SArnaud Ebalard u8 fan_sta; /* bit 0: set when actual fan speed is more than 148594fbe71SArnaud Ebalard * 25% outside requested fan speed 149594fbe71SArnaud Ebalard * bit 1: set when no transition occurs on fan 150594fbe71SArnaud Ebalard * pin for 0.7s 151594fbe71SArnaud Ebalard */ 152594fbe71SArnaud Ebalard u8 set_out; /* controls fan rotation speed in open-loop mode */ 153594fbe71SArnaud Ebalard u8 fan_cmd1; /* 0: FG_PLS_ID0 FG pulses count per revolution 154594fbe71SArnaud Ebalard * 0: 2 counts per revolution 155594fbe71SArnaud Ebalard * 1: 4 counts per revolution 156594fbe71SArnaud Ebalard * 1: PWM_POLARITY 1: negative_duty 157594fbe71SArnaud Ebalard * 0: positive_duty 158594fbe71SArnaud Ebalard * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1] 159594fbe71SArnaud Ebalard * 00: Divide fan clock by 1 160594fbe71SArnaud Ebalard * 01: Divide fan clock by 2 161594fbe71SArnaud Ebalard * 10: Divide fan clock by 4 162594fbe71SArnaud Ebalard * 11: Divide fan clock by 8 163594fbe71SArnaud Ebalard * 4: FAN_MODE 1:closed-loop, 0:open-loop 164594fbe71SArnaud Ebalard * 5: OUT_MODE 1:PWM, 0:DC 165594fbe71SArnaud Ebalard * 6: DET_FAN_OOC enable "fan ooc" status 166594fbe71SArnaud Ebalard * 7: DET_FAN_FAIL enable "fan fail" status 167594fbe71SArnaud Ebalard */ 168594fbe71SArnaud Ebalard u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code 169594fbe71SArnaud Ebalard * 2,3: FG_GEAR_MODE 170594fbe71SArnaud Ebalard * 00: multiplier = 1 171594fbe71SArnaud Ebalard * 01: multiplier = 2 172594fbe71SArnaud Ebalard * 10: multiplier = 4 173594fbe71SArnaud Ebalard * 4: Mask ALERT# (g763 only) 174594fbe71SArnaud Ebalard */ 175594fbe71SArnaud Ebalard }; 176594fbe71SArnaud Ebalard 177594fbe71SArnaud Ebalard /* 178594fbe71SArnaud Ebalard * Convert count value from fan controller register (FAN_SET_CNT) into fan 179594fbe71SArnaud Ebalard * speed RPM value. Note that the datasheet documents a basic formula; 180594fbe71SArnaud Ebalard * influence of additional parameters (fan clock divisor, fan gear mode) 181594fbe71SArnaud Ebalard * have been infered from examples in the datasheet and tests. 182594fbe71SArnaud Ebalard */ 183594fbe71SArnaud Ebalard static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p, 184594fbe71SArnaud Ebalard u8 clk_div, u8 gear_mult) 185594fbe71SArnaud Ebalard { 186594fbe71SArnaud Ebalard if (cnt == 0xff) /* setting cnt to 255 stops the fan */ 187594fbe71SArnaud Ebalard return 0; 188594fbe71SArnaud Ebalard 189594fbe71SArnaud Ebalard return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div); 190594fbe71SArnaud Ebalard } 191594fbe71SArnaud Ebalard 192594fbe71SArnaud Ebalard /* 193594fbe71SArnaud Ebalard * Convert fan RPM value from sysfs into count value for fan controller 194594fbe71SArnaud Ebalard * register (FAN_SET_CNT). 195594fbe71SArnaud Ebalard */ 196594fbe71SArnaud Ebalard static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p, 197594fbe71SArnaud Ebalard u8 clk_div, u8 gear_mult) 198594fbe71SArnaud Ebalard { 199594fbe71SArnaud Ebalard if (!rpm) /* to stop the fan, set cnt to 255 */ 200594fbe71SArnaud Ebalard return 0xff; 201594fbe71SArnaud Ebalard 202594fbe71SArnaud Ebalard return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)), 203594fbe71SArnaud Ebalard 0, 255); 204594fbe71SArnaud Ebalard } 205594fbe71SArnaud Ebalard 206594fbe71SArnaud Ebalard /* helper to grab and cache data, at most one time per second */ 207594fbe71SArnaud Ebalard static struct g762_data *g762_update_client(struct device *dev) 208594fbe71SArnaud Ebalard { 209398e16dbSAxel Lin struct g762_data *data = dev_get_drvdata(dev); 210398e16dbSAxel Lin struct i2c_client *client = data->client; 211594fbe71SArnaud Ebalard int ret = 0; 212594fbe71SArnaud Ebalard 213594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 214594fbe71SArnaud Ebalard if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) && 215594fbe71SArnaud Ebalard likely(data->valid)) 216594fbe71SArnaud Ebalard goto out; 217594fbe71SArnaud Ebalard 218594fbe71SArnaud Ebalard ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT); 219594fbe71SArnaud Ebalard if (ret < 0) 220594fbe71SArnaud Ebalard goto out; 221594fbe71SArnaud Ebalard data->set_cnt = ret; 222594fbe71SArnaud Ebalard 223594fbe71SArnaud Ebalard ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT); 224594fbe71SArnaud Ebalard if (ret < 0) 225594fbe71SArnaud Ebalard goto out; 226594fbe71SArnaud Ebalard data->act_cnt = ret; 227594fbe71SArnaud Ebalard 228594fbe71SArnaud Ebalard ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA); 229594fbe71SArnaud Ebalard if (ret < 0) 230594fbe71SArnaud Ebalard goto out; 231594fbe71SArnaud Ebalard data->fan_sta = ret; 232594fbe71SArnaud Ebalard 233594fbe71SArnaud Ebalard ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT); 234594fbe71SArnaud Ebalard if (ret < 0) 235594fbe71SArnaud Ebalard goto out; 236594fbe71SArnaud Ebalard data->set_out = ret; 237594fbe71SArnaud Ebalard 238594fbe71SArnaud Ebalard ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1); 239594fbe71SArnaud Ebalard if (ret < 0) 240594fbe71SArnaud Ebalard goto out; 241594fbe71SArnaud Ebalard data->fan_cmd1 = ret; 242594fbe71SArnaud Ebalard 243594fbe71SArnaud Ebalard ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2); 244594fbe71SArnaud Ebalard if (ret < 0) 245594fbe71SArnaud Ebalard goto out; 246594fbe71SArnaud Ebalard data->fan_cmd2 = ret; 247594fbe71SArnaud Ebalard 248594fbe71SArnaud Ebalard data->last_updated = jiffies; 249594fbe71SArnaud Ebalard data->valid = true; 250594fbe71SArnaud Ebalard out: 251594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 252594fbe71SArnaud Ebalard 253594fbe71SArnaud Ebalard if (ret < 0) /* upon error, encode it in return value */ 254594fbe71SArnaud Ebalard data = ERR_PTR(ret); 255594fbe71SArnaud Ebalard 256594fbe71SArnaud Ebalard return data; 257594fbe71SArnaud Ebalard } 258594fbe71SArnaud Ebalard 259594fbe71SArnaud Ebalard /* helpers for writing hardware parameters */ 260594fbe71SArnaud Ebalard 261594fbe71SArnaud Ebalard /* 262594fbe71SArnaud Ebalard * Set input clock frequency received on CLK pin of the chip. Accepted values 263594fbe71SArnaud Ebalard * are between 0 and 0xffffff. If zero is given, then default frequency 264594fbe71SArnaud Ebalard * (32,768Hz) is used. Note that clock frequency is a characteristic of the 265594fbe71SArnaud Ebalard * system but an internal parameter, i.e. value is not passed to the device. 266594fbe71SArnaud Ebalard */ 267594fbe71SArnaud Ebalard static int do_set_clk_freq(struct device *dev, unsigned long val) 268594fbe71SArnaud Ebalard { 269398e16dbSAxel Lin struct g762_data *data = dev_get_drvdata(dev); 270594fbe71SArnaud Ebalard 271594fbe71SArnaud Ebalard if (val > 0xffffff) 272594fbe71SArnaud Ebalard return -EINVAL; 273594fbe71SArnaud Ebalard if (!val) 274594fbe71SArnaud Ebalard val = 32768; 275594fbe71SArnaud Ebalard 276594fbe71SArnaud Ebalard data->clk_freq = val; 277594fbe71SArnaud Ebalard 278594fbe71SArnaud Ebalard return 0; 279594fbe71SArnaud Ebalard } 280594fbe71SArnaud Ebalard 281594fbe71SArnaud Ebalard /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ 282594fbe71SArnaud Ebalard static int do_set_pwm_mode(struct device *dev, unsigned long val) 283594fbe71SArnaud Ebalard { 284594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 285594fbe71SArnaud Ebalard int ret; 286594fbe71SArnaud Ebalard 287594fbe71SArnaud Ebalard if (IS_ERR(data)) 288594fbe71SArnaud Ebalard return PTR_ERR(data); 289594fbe71SArnaud Ebalard 290594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 291594fbe71SArnaud Ebalard switch (val) { 292594fbe71SArnaud Ebalard case G762_OUT_MODE_PWM: 293594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE; 294594fbe71SArnaud Ebalard break; 295594fbe71SArnaud Ebalard case G762_OUT_MODE_DC: 296594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE; 297594fbe71SArnaud Ebalard break; 298594fbe71SArnaud Ebalard default: 299594fbe71SArnaud Ebalard ret = -EINVAL; 300594fbe71SArnaud Ebalard goto out; 301594fbe71SArnaud Ebalard } 302398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 303594fbe71SArnaud Ebalard data->fan_cmd1); 304594fbe71SArnaud Ebalard data->valid = false; 305594fbe71SArnaud Ebalard out: 306594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 307594fbe71SArnaud Ebalard 308594fbe71SArnaud Ebalard return ret; 309594fbe71SArnaud Ebalard } 310594fbe71SArnaud Ebalard 311594fbe71SArnaud Ebalard /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */ 312594fbe71SArnaud Ebalard static int do_set_fan_div(struct device *dev, unsigned long val) 313594fbe71SArnaud Ebalard { 314594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 315594fbe71SArnaud Ebalard int ret; 316594fbe71SArnaud Ebalard 317594fbe71SArnaud Ebalard if (IS_ERR(data)) 318594fbe71SArnaud Ebalard return PTR_ERR(data); 319594fbe71SArnaud Ebalard 320594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 321594fbe71SArnaud Ebalard switch (val) { 322594fbe71SArnaud Ebalard case 1: 323594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0; 324594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1; 325594fbe71SArnaud Ebalard break; 326594fbe71SArnaud Ebalard case 2: 327594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0; 328594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1; 329594fbe71SArnaud Ebalard break; 330594fbe71SArnaud Ebalard case 4: 331594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0; 332594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1; 333594fbe71SArnaud Ebalard break; 334594fbe71SArnaud Ebalard case 8: 335594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0; 336594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1; 337594fbe71SArnaud Ebalard break; 338594fbe71SArnaud Ebalard default: 339594fbe71SArnaud Ebalard ret = -EINVAL; 340594fbe71SArnaud Ebalard goto out; 341594fbe71SArnaud Ebalard } 342398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 343594fbe71SArnaud Ebalard data->fan_cmd1); 344594fbe71SArnaud Ebalard data->valid = false; 345594fbe71SArnaud Ebalard out: 346594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 347594fbe71SArnaud Ebalard 348594fbe71SArnaud Ebalard return ret; 349594fbe71SArnaud Ebalard } 350594fbe71SArnaud Ebalard 351594fbe71SArnaud Ebalard /* Set fan gear mode. Accepts either 0, 1 or 2. */ 352594fbe71SArnaud Ebalard static int do_set_fan_gear_mode(struct device *dev, unsigned long val) 353594fbe71SArnaud Ebalard { 354594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 355594fbe71SArnaud Ebalard int ret; 356594fbe71SArnaud Ebalard 357594fbe71SArnaud Ebalard if (IS_ERR(data)) 358594fbe71SArnaud Ebalard return PTR_ERR(data); 359594fbe71SArnaud Ebalard 360594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 361594fbe71SArnaud Ebalard switch (val) { 362594fbe71SArnaud Ebalard case 0: 363594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0; 364594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1; 365594fbe71SArnaud Ebalard break; 366594fbe71SArnaud Ebalard case 1: 367594fbe71SArnaud Ebalard data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0; 368594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1; 369594fbe71SArnaud Ebalard break; 370594fbe71SArnaud Ebalard case 2: 371594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0; 372594fbe71SArnaud Ebalard data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1; 373594fbe71SArnaud Ebalard break; 374594fbe71SArnaud Ebalard default: 375594fbe71SArnaud Ebalard ret = -EINVAL; 376594fbe71SArnaud Ebalard goto out; 377594fbe71SArnaud Ebalard } 378398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, 379594fbe71SArnaud Ebalard data->fan_cmd2); 380594fbe71SArnaud Ebalard data->valid = false; 381594fbe71SArnaud Ebalard out: 382594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 383594fbe71SArnaud Ebalard 384594fbe71SArnaud Ebalard return ret; 385594fbe71SArnaud Ebalard } 386594fbe71SArnaud Ebalard 387594fbe71SArnaud Ebalard /* Set number of fan pulses per revolution. Accepts either 2 or 4. */ 388594fbe71SArnaud Ebalard static int do_set_fan_pulses(struct device *dev, unsigned long val) 389594fbe71SArnaud Ebalard { 390594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 391594fbe71SArnaud Ebalard int ret; 392594fbe71SArnaud Ebalard 393594fbe71SArnaud Ebalard if (IS_ERR(data)) 394594fbe71SArnaud Ebalard return PTR_ERR(data); 395594fbe71SArnaud Ebalard 396594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 397594fbe71SArnaud Ebalard switch (val) { 398594fbe71SArnaud Ebalard case 2: 399594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV; 400594fbe71SArnaud Ebalard break; 401594fbe71SArnaud Ebalard case 4: 402594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV; 403594fbe71SArnaud Ebalard break; 404594fbe71SArnaud Ebalard default: 405594fbe71SArnaud Ebalard ret = -EINVAL; 406594fbe71SArnaud Ebalard goto out; 407594fbe71SArnaud Ebalard } 408398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 409594fbe71SArnaud Ebalard data->fan_cmd1); 410594fbe71SArnaud Ebalard data->valid = false; 411594fbe71SArnaud Ebalard out: 412594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 413594fbe71SArnaud Ebalard 414594fbe71SArnaud Ebalard return ret; 415594fbe71SArnaud Ebalard } 416594fbe71SArnaud Ebalard 417594fbe71SArnaud Ebalard /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ 418594fbe71SArnaud Ebalard static int do_set_pwm_enable(struct device *dev, unsigned long val) 419594fbe71SArnaud Ebalard { 420594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 421594fbe71SArnaud Ebalard int ret; 422594fbe71SArnaud Ebalard 423594fbe71SArnaud Ebalard if (IS_ERR(data)) 424594fbe71SArnaud Ebalard return PTR_ERR(data); 425594fbe71SArnaud Ebalard 426594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 427594fbe71SArnaud Ebalard switch (val) { 428594fbe71SArnaud Ebalard case G762_FAN_MODE_CLOSED_LOOP: 429594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE; 430594fbe71SArnaud Ebalard break; 431594fbe71SArnaud Ebalard case G762_FAN_MODE_OPEN_LOOP: 432594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE; 433594fbe71SArnaud Ebalard /* 434594fbe71SArnaud Ebalard * BUG FIX: if SET_CNT register value is 255 then, for some 435594fbe71SArnaud Ebalard * unknown reason, fan will not rotate as expected, no matter 436594fbe71SArnaud Ebalard * the value of SET_OUT (to be specific, this seems to happen 437594fbe71SArnaud Ebalard * only in PWM mode). To workaround this bug, we give SET_CNT 438594fbe71SArnaud Ebalard * value of 254 if it is 255 when switching to open-loop. 439594fbe71SArnaud Ebalard */ 440594fbe71SArnaud Ebalard if (data->set_cnt == 0xff) 441398e16dbSAxel Lin i2c_smbus_write_byte_data(data->client, 442398e16dbSAxel Lin G762_REG_SET_CNT, 254); 443594fbe71SArnaud Ebalard break; 444594fbe71SArnaud Ebalard default: 445594fbe71SArnaud Ebalard ret = -EINVAL; 446594fbe71SArnaud Ebalard goto out; 447594fbe71SArnaud Ebalard } 448594fbe71SArnaud Ebalard 449398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 450594fbe71SArnaud Ebalard data->fan_cmd1); 451594fbe71SArnaud Ebalard data->valid = false; 452594fbe71SArnaud Ebalard out: 453594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 454594fbe71SArnaud Ebalard 455594fbe71SArnaud Ebalard return ret; 456594fbe71SArnaud Ebalard } 457594fbe71SArnaud Ebalard 458594fbe71SArnaud Ebalard /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */ 459594fbe71SArnaud Ebalard static int do_set_pwm_polarity(struct device *dev, unsigned long val) 460594fbe71SArnaud Ebalard { 461594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 462594fbe71SArnaud Ebalard int ret; 463594fbe71SArnaud Ebalard 464594fbe71SArnaud Ebalard if (IS_ERR(data)) 465594fbe71SArnaud Ebalard return PTR_ERR(data); 466594fbe71SArnaud Ebalard 467594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 468594fbe71SArnaud Ebalard switch (val) { 469594fbe71SArnaud Ebalard case G762_PWM_POLARITY_POSITIVE: 470594fbe71SArnaud Ebalard data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY; 471594fbe71SArnaud Ebalard break; 472594fbe71SArnaud Ebalard case G762_PWM_POLARITY_NEGATIVE: 473594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY; 474594fbe71SArnaud Ebalard break; 475594fbe71SArnaud Ebalard default: 476594fbe71SArnaud Ebalard ret = -EINVAL; 477594fbe71SArnaud Ebalard goto out; 478594fbe71SArnaud Ebalard } 479398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 480594fbe71SArnaud Ebalard data->fan_cmd1); 481594fbe71SArnaud Ebalard data->valid = false; 482594fbe71SArnaud Ebalard out: 483594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 484594fbe71SArnaud Ebalard 485594fbe71SArnaud Ebalard return ret; 486594fbe71SArnaud Ebalard } 487594fbe71SArnaud Ebalard 488594fbe71SArnaud Ebalard /* 489594fbe71SArnaud Ebalard * Set pwm value. Accepts values between 0 (stops the fan) and 490594fbe71SArnaud Ebalard * 255 (full speed). This only makes sense in open-loop mode. 491594fbe71SArnaud Ebalard */ 492594fbe71SArnaud Ebalard static int do_set_pwm(struct device *dev, unsigned long val) 493594fbe71SArnaud Ebalard { 494398e16dbSAxel Lin struct g762_data *data = dev_get_drvdata(dev); 495398e16dbSAxel Lin struct i2c_client *client = data->client; 496594fbe71SArnaud Ebalard int ret; 497594fbe71SArnaud Ebalard 498594fbe71SArnaud Ebalard if (val > 255) 499594fbe71SArnaud Ebalard return -EINVAL; 500594fbe71SArnaud Ebalard 501594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 502594fbe71SArnaud Ebalard ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val); 503594fbe71SArnaud Ebalard data->valid = false; 504594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 505594fbe71SArnaud Ebalard 506594fbe71SArnaud Ebalard return ret; 507594fbe71SArnaud Ebalard } 508594fbe71SArnaud Ebalard 509594fbe71SArnaud Ebalard /* 510594fbe71SArnaud Ebalard * Set fan RPM value. Can be called both in closed and open-loop mode 511594fbe71SArnaud Ebalard * but effect will only be seen after closed-loop mode is configured. 512594fbe71SArnaud Ebalard */ 513594fbe71SArnaud Ebalard static int do_set_fan_target(struct device *dev, unsigned long val) 514594fbe71SArnaud Ebalard { 515594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 516594fbe71SArnaud Ebalard int ret; 517594fbe71SArnaud Ebalard 518594fbe71SArnaud Ebalard if (IS_ERR(data)) 519594fbe71SArnaud Ebalard return PTR_ERR(data); 520594fbe71SArnaud Ebalard 521594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 522594fbe71SArnaud Ebalard data->set_cnt = cnt_from_rpm(val, data->clk_freq, 523594fbe71SArnaud Ebalard G762_PULSE_FROM_REG(data->fan_cmd1), 524594fbe71SArnaud Ebalard G762_CLKDIV_FROM_REG(data->fan_cmd1), 525594fbe71SArnaud Ebalard G762_GEARMULT_FROM_REG(data->fan_cmd2)); 526398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT, 527594fbe71SArnaud Ebalard data->set_cnt); 528594fbe71SArnaud Ebalard data->valid = false; 529594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 530594fbe71SArnaud Ebalard 531594fbe71SArnaud Ebalard return ret; 532594fbe71SArnaud Ebalard } 533594fbe71SArnaud Ebalard 534594fbe71SArnaud Ebalard /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */ 535594fbe71SArnaud Ebalard static int do_set_fan_startv(struct device *dev, unsigned long val) 536594fbe71SArnaud Ebalard { 537594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 538594fbe71SArnaud Ebalard int ret; 539594fbe71SArnaud Ebalard 540594fbe71SArnaud Ebalard if (IS_ERR(data)) 541594fbe71SArnaud Ebalard return PTR_ERR(data); 542594fbe71SArnaud Ebalard 543594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 544594fbe71SArnaud Ebalard switch (val) { 545594fbe71SArnaud Ebalard case 0: 546594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0; 547594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1; 548594fbe71SArnaud Ebalard break; 549594fbe71SArnaud Ebalard case 1: 550594fbe71SArnaud Ebalard data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0; 551594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1; 552594fbe71SArnaud Ebalard break; 553594fbe71SArnaud Ebalard case 2: 554594fbe71SArnaud Ebalard data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0; 555594fbe71SArnaud Ebalard data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1; 556594fbe71SArnaud Ebalard break; 557594fbe71SArnaud Ebalard case 3: 558594fbe71SArnaud Ebalard data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0; 559594fbe71SArnaud Ebalard data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1; 560594fbe71SArnaud Ebalard break; 561594fbe71SArnaud Ebalard default: 562594fbe71SArnaud Ebalard ret = -EINVAL; 563594fbe71SArnaud Ebalard goto out; 564594fbe71SArnaud Ebalard } 565398e16dbSAxel Lin ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, 566594fbe71SArnaud Ebalard data->fan_cmd2); 567594fbe71SArnaud Ebalard data->valid = false; 568594fbe71SArnaud Ebalard out: 569594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 570594fbe71SArnaud Ebalard 571594fbe71SArnaud Ebalard return ret; 572594fbe71SArnaud Ebalard } 573594fbe71SArnaud Ebalard 574594fbe71SArnaud Ebalard /* 575594fbe71SArnaud Ebalard * Helper to import hardware characteristics from .dts file and push 576594fbe71SArnaud Ebalard * those to the chip. 577594fbe71SArnaud Ebalard */ 578594fbe71SArnaud Ebalard 579594fbe71SArnaud Ebalard #ifdef CONFIG_OF 580adaa50b0SJingoo Han static const struct of_device_id g762_dt_match[] = { 581594fbe71SArnaud Ebalard { .compatible = "gmt,g762" }, 582594fbe71SArnaud Ebalard { .compatible = "gmt,g763" }, 583594fbe71SArnaud Ebalard { }, 584594fbe71SArnaud Ebalard }; 585*de66b380SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, g762_dt_match); 586594fbe71SArnaud Ebalard 587594fbe71SArnaud Ebalard /* 588594fbe71SArnaud Ebalard * Grab clock (a required property), enable it, get (fixed) clock frequency 589594fbe71SArnaud Ebalard * and store it. Note: upon success, clock has been prepared and enabled; it 590594fbe71SArnaud Ebalard * must later be unprepared and disabled (e.g. during module unloading) by a 591594fbe71SArnaud Ebalard * call to g762_of_clock_disable(). Note that a reference to clock is kept 592594fbe71SArnaud Ebalard * in our private data structure to be used in this function. 593594fbe71SArnaud Ebalard */ 594594fbe71SArnaud Ebalard static int g762_of_clock_enable(struct i2c_client *client) 595594fbe71SArnaud Ebalard { 596594fbe71SArnaud Ebalard struct g762_data *data; 597594fbe71SArnaud Ebalard unsigned long clk_freq; 598594fbe71SArnaud Ebalard struct clk *clk; 599594fbe71SArnaud Ebalard int ret; 600594fbe71SArnaud Ebalard 601594fbe71SArnaud Ebalard if (!client->dev.of_node) 602594fbe71SArnaud Ebalard return 0; 603594fbe71SArnaud Ebalard 604594fbe71SArnaud Ebalard clk = of_clk_get(client->dev.of_node, 0); 605594fbe71SArnaud Ebalard if (IS_ERR(clk)) { 606594fbe71SArnaud Ebalard dev_err(&client->dev, "failed to get clock\n"); 607594fbe71SArnaud Ebalard return PTR_ERR(clk); 608594fbe71SArnaud Ebalard } 609594fbe71SArnaud Ebalard 610594fbe71SArnaud Ebalard ret = clk_prepare_enable(clk); 611594fbe71SArnaud Ebalard if (ret) { 612594fbe71SArnaud Ebalard dev_err(&client->dev, "failed to enable clock\n"); 613594fbe71SArnaud Ebalard goto clk_put; 614594fbe71SArnaud Ebalard } 615594fbe71SArnaud Ebalard 616594fbe71SArnaud Ebalard clk_freq = clk_get_rate(clk); 617594fbe71SArnaud Ebalard ret = do_set_clk_freq(&client->dev, clk_freq); 618594fbe71SArnaud Ebalard if (ret) { 619594fbe71SArnaud Ebalard dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq); 620594fbe71SArnaud Ebalard goto clk_unprep; 621594fbe71SArnaud Ebalard } 622594fbe71SArnaud Ebalard 623594fbe71SArnaud Ebalard data = i2c_get_clientdata(client); 624594fbe71SArnaud Ebalard data->clk = clk; 625594fbe71SArnaud Ebalard 626594fbe71SArnaud Ebalard return 0; 627594fbe71SArnaud Ebalard 628594fbe71SArnaud Ebalard clk_unprep: 629594fbe71SArnaud Ebalard clk_disable_unprepare(clk); 630594fbe71SArnaud Ebalard 631594fbe71SArnaud Ebalard clk_put: 632594fbe71SArnaud Ebalard clk_put(clk); 633594fbe71SArnaud Ebalard 634594fbe71SArnaud Ebalard return ret; 635594fbe71SArnaud Ebalard } 636594fbe71SArnaud Ebalard 637594fbe71SArnaud Ebalard static void g762_of_clock_disable(struct i2c_client *client) 638594fbe71SArnaud Ebalard { 639594fbe71SArnaud Ebalard struct g762_data *data = i2c_get_clientdata(client); 640594fbe71SArnaud Ebalard 641594fbe71SArnaud Ebalard if (!data->clk) 642594fbe71SArnaud Ebalard return; 643594fbe71SArnaud Ebalard 644594fbe71SArnaud Ebalard clk_disable_unprepare(data->clk); 645594fbe71SArnaud Ebalard clk_put(data->clk); 646594fbe71SArnaud Ebalard } 647594fbe71SArnaud Ebalard 648594fbe71SArnaud Ebalard static int g762_of_prop_import_one(struct i2c_client *client, 649594fbe71SArnaud Ebalard const char *pname, 650594fbe71SArnaud Ebalard int (*psetter)(struct device *dev, 651594fbe71SArnaud Ebalard unsigned long val)) 652594fbe71SArnaud Ebalard { 653fce9626cSAxel Lin int ret; 654594fbe71SArnaud Ebalard u32 pval; 655594fbe71SArnaud Ebalard 656fce9626cSAxel Lin if (of_property_read_u32(client->dev.of_node, pname, &pval)) 657594fbe71SArnaud Ebalard return 0; 658594fbe71SArnaud Ebalard 659594fbe71SArnaud Ebalard dev_dbg(&client->dev, "found %s (%d)\n", pname, pval); 660594fbe71SArnaud Ebalard ret = (*psetter)(&client->dev, pval); 661594fbe71SArnaud Ebalard if (ret) 662594fbe71SArnaud Ebalard dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval); 663594fbe71SArnaud Ebalard 664594fbe71SArnaud Ebalard return ret; 665594fbe71SArnaud Ebalard } 666594fbe71SArnaud Ebalard 667594fbe71SArnaud Ebalard static int g762_of_prop_import(struct i2c_client *client) 668594fbe71SArnaud Ebalard { 669594fbe71SArnaud Ebalard int ret; 670594fbe71SArnaud Ebalard 671594fbe71SArnaud Ebalard if (!client->dev.of_node) 672594fbe71SArnaud Ebalard return 0; 673594fbe71SArnaud Ebalard 674594fbe71SArnaud Ebalard ret = g762_of_prop_import_one(client, "fan_gear_mode", 675594fbe71SArnaud Ebalard do_set_fan_gear_mode); 676594fbe71SArnaud Ebalard if (ret) 677594fbe71SArnaud Ebalard return ret; 678594fbe71SArnaud Ebalard 679594fbe71SArnaud Ebalard ret = g762_of_prop_import_one(client, "pwm_polarity", 680594fbe71SArnaud Ebalard do_set_pwm_polarity); 681594fbe71SArnaud Ebalard if (ret) 682594fbe71SArnaud Ebalard return ret; 683594fbe71SArnaud Ebalard 684594fbe71SArnaud Ebalard return g762_of_prop_import_one(client, "fan_startv", 685594fbe71SArnaud Ebalard do_set_fan_startv); 686594fbe71SArnaud Ebalard } 687594fbe71SArnaud Ebalard 688594fbe71SArnaud Ebalard #else 689594fbe71SArnaud Ebalard static int g762_of_prop_import(struct i2c_client *client) 690594fbe71SArnaud Ebalard { 691594fbe71SArnaud Ebalard return 0; 692594fbe71SArnaud Ebalard } 693594fbe71SArnaud Ebalard 694594fbe71SArnaud Ebalard static int g762_of_clock_enable(struct i2c_client *client) 695594fbe71SArnaud Ebalard { 696594fbe71SArnaud Ebalard return 0; 697594fbe71SArnaud Ebalard } 698594fbe71SArnaud Ebalard 699594fbe71SArnaud Ebalard static void g762_of_clock_disable(struct i2c_client *client) { } 700594fbe71SArnaud Ebalard #endif 701594fbe71SArnaud Ebalard 702594fbe71SArnaud Ebalard /* 703594fbe71SArnaud Ebalard * Helper to import hardware characteristics from .dts file and push 704594fbe71SArnaud Ebalard * those to the chip. 705594fbe71SArnaud Ebalard */ 706594fbe71SArnaud Ebalard 707594fbe71SArnaud Ebalard static int g762_pdata_prop_import(struct i2c_client *client) 708594fbe71SArnaud Ebalard { 709a8b3a3a5SJingoo Han struct g762_platform_data *pdata = dev_get_platdata(&client->dev); 710594fbe71SArnaud Ebalard int ret; 711594fbe71SArnaud Ebalard 712594fbe71SArnaud Ebalard if (!pdata) 713594fbe71SArnaud Ebalard return 0; 714594fbe71SArnaud Ebalard 715594fbe71SArnaud Ebalard ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode); 716594fbe71SArnaud Ebalard if (ret) 717594fbe71SArnaud Ebalard return ret; 718594fbe71SArnaud Ebalard 719594fbe71SArnaud Ebalard ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity); 720594fbe71SArnaud Ebalard if (ret) 721594fbe71SArnaud Ebalard return ret; 722594fbe71SArnaud Ebalard 723594fbe71SArnaud Ebalard ret = do_set_fan_startv(&client->dev, pdata->fan_startv); 724594fbe71SArnaud Ebalard if (ret) 725594fbe71SArnaud Ebalard return ret; 726594fbe71SArnaud Ebalard 727594fbe71SArnaud Ebalard return do_set_clk_freq(&client->dev, pdata->clk_freq); 728594fbe71SArnaud Ebalard } 729594fbe71SArnaud Ebalard 730594fbe71SArnaud Ebalard /* 731594fbe71SArnaud Ebalard * sysfs attributes 732594fbe71SArnaud Ebalard */ 733594fbe71SArnaud Ebalard 734594fbe71SArnaud Ebalard /* 735594fbe71SArnaud Ebalard * Read function for fan1_input sysfs file. Return current fan RPM value, or 736594fbe71SArnaud Ebalard * 0 if fan is out of control. 737594fbe71SArnaud Ebalard */ 738594fbe71SArnaud Ebalard static ssize_t get_fan_rpm(struct device *dev, struct device_attribute *da, 739594fbe71SArnaud Ebalard char *buf) 740594fbe71SArnaud Ebalard { 741594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 742594fbe71SArnaud Ebalard unsigned int rpm = 0; 743594fbe71SArnaud Ebalard 744594fbe71SArnaud Ebalard if (IS_ERR(data)) 745594fbe71SArnaud Ebalard return PTR_ERR(data); 746594fbe71SArnaud Ebalard 747594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 748594fbe71SArnaud Ebalard /* reverse logic: fan out of control reporting is enabled low */ 749594fbe71SArnaud Ebalard if (data->fan_sta & G762_REG_FAN_STA_OOC) { 750594fbe71SArnaud Ebalard rpm = rpm_from_cnt(data->act_cnt, data->clk_freq, 751594fbe71SArnaud Ebalard G762_PULSE_FROM_REG(data->fan_cmd1), 752594fbe71SArnaud Ebalard G762_CLKDIV_FROM_REG(data->fan_cmd1), 753594fbe71SArnaud Ebalard G762_GEARMULT_FROM_REG(data->fan_cmd2)); 754594fbe71SArnaud Ebalard } 755594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 756594fbe71SArnaud Ebalard 757594fbe71SArnaud Ebalard return sprintf(buf, "%u\n", rpm); 758594fbe71SArnaud Ebalard } 759594fbe71SArnaud Ebalard 760594fbe71SArnaud Ebalard /* 761594fbe71SArnaud Ebalard * Read and write functions for pwm1_mode sysfs file. Get and set fan speed 762594fbe71SArnaud Ebalard * control mode i.e. PWM (1) or DC (0). 763594fbe71SArnaud Ebalard */ 764594fbe71SArnaud Ebalard static ssize_t get_pwm_mode(struct device *dev, struct device_attribute *da, 765594fbe71SArnaud Ebalard char *buf) 766594fbe71SArnaud Ebalard { 767594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 768594fbe71SArnaud Ebalard 769594fbe71SArnaud Ebalard if (IS_ERR(data)) 770594fbe71SArnaud Ebalard return PTR_ERR(data); 771594fbe71SArnaud Ebalard 772594fbe71SArnaud Ebalard return sprintf(buf, "%d\n", 773594fbe71SArnaud Ebalard !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE)); 774594fbe71SArnaud Ebalard } 775594fbe71SArnaud Ebalard 776594fbe71SArnaud Ebalard static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *da, 777594fbe71SArnaud Ebalard const char *buf, size_t count) 778594fbe71SArnaud Ebalard { 779594fbe71SArnaud Ebalard unsigned long val; 780594fbe71SArnaud Ebalard int ret; 781594fbe71SArnaud Ebalard 782594fbe71SArnaud Ebalard if (kstrtoul(buf, 10, &val)) 783594fbe71SArnaud Ebalard return -EINVAL; 784594fbe71SArnaud Ebalard 785594fbe71SArnaud Ebalard ret = do_set_pwm_mode(dev, val); 786594fbe71SArnaud Ebalard if (ret < 0) 787594fbe71SArnaud Ebalard return ret; 788594fbe71SArnaud Ebalard 789594fbe71SArnaud Ebalard return count; 790594fbe71SArnaud Ebalard } 791594fbe71SArnaud Ebalard 792594fbe71SArnaud Ebalard /* 793594fbe71SArnaud Ebalard * Read and write functions for fan1_div sysfs file. Get and set fan 794594fbe71SArnaud Ebalard * controller prescaler value 795594fbe71SArnaud Ebalard */ 796594fbe71SArnaud Ebalard static ssize_t get_fan_div(struct device *dev, 797594fbe71SArnaud Ebalard struct device_attribute *da, char *buf) 798594fbe71SArnaud Ebalard { 799594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 800594fbe71SArnaud Ebalard 801594fbe71SArnaud Ebalard if (IS_ERR(data)) 802594fbe71SArnaud Ebalard return PTR_ERR(data); 803594fbe71SArnaud Ebalard 804594fbe71SArnaud Ebalard return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1)); 805594fbe71SArnaud Ebalard } 806594fbe71SArnaud Ebalard 807594fbe71SArnaud Ebalard static ssize_t set_fan_div(struct device *dev, 808594fbe71SArnaud Ebalard struct device_attribute *da, 809594fbe71SArnaud Ebalard const char *buf, size_t count) 810594fbe71SArnaud Ebalard { 811594fbe71SArnaud Ebalard unsigned long val; 812594fbe71SArnaud Ebalard int ret; 813594fbe71SArnaud Ebalard 814594fbe71SArnaud Ebalard if (kstrtoul(buf, 10, &val)) 815594fbe71SArnaud Ebalard return -EINVAL; 816594fbe71SArnaud Ebalard 817594fbe71SArnaud Ebalard ret = do_set_fan_div(dev, val); 818594fbe71SArnaud Ebalard if (ret < 0) 819594fbe71SArnaud Ebalard return ret; 820594fbe71SArnaud Ebalard 821594fbe71SArnaud Ebalard return count; 822594fbe71SArnaud Ebalard } 823594fbe71SArnaud Ebalard 824594fbe71SArnaud Ebalard /* 825594fbe71SArnaud Ebalard * Read and write functions for fan1_pulses sysfs file. Get and set number 826594fbe71SArnaud Ebalard * of tachometer pulses per fan revolution. 827594fbe71SArnaud Ebalard */ 828594fbe71SArnaud Ebalard static ssize_t get_fan_pulses(struct device *dev, 829594fbe71SArnaud Ebalard struct device_attribute *da, char *buf) 830594fbe71SArnaud Ebalard { 831594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 832594fbe71SArnaud Ebalard 833594fbe71SArnaud Ebalard if (IS_ERR(data)) 834594fbe71SArnaud Ebalard return PTR_ERR(data); 835594fbe71SArnaud Ebalard 836594fbe71SArnaud Ebalard return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1)); 837594fbe71SArnaud Ebalard } 838594fbe71SArnaud Ebalard 839594fbe71SArnaud Ebalard static ssize_t set_fan_pulses(struct device *dev, 840594fbe71SArnaud Ebalard struct device_attribute *da, 841594fbe71SArnaud Ebalard const char *buf, size_t count) 842594fbe71SArnaud Ebalard { 843594fbe71SArnaud Ebalard unsigned long val; 844594fbe71SArnaud Ebalard int ret; 845594fbe71SArnaud Ebalard 846594fbe71SArnaud Ebalard if (kstrtoul(buf, 10, &val)) 847594fbe71SArnaud Ebalard return -EINVAL; 848594fbe71SArnaud Ebalard 849594fbe71SArnaud Ebalard ret = do_set_fan_pulses(dev, val); 850594fbe71SArnaud Ebalard if (ret < 0) 851594fbe71SArnaud Ebalard return ret; 852594fbe71SArnaud Ebalard 853594fbe71SArnaud Ebalard return count; 854594fbe71SArnaud Ebalard } 855594fbe71SArnaud Ebalard 856594fbe71SArnaud Ebalard /* 857594fbe71SArnaud Ebalard * Read and write functions for pwm1_enable. Get and set fan speed control mode 858594fbe71SArnaud Ebalard * (i.e. closed or open-loop). 859594fbe71SArnaud Ebalard * 860594fbe71SArnaud Ebalard * Following documentation about hwmon's sysfs interface, a pwm1_enable node 861594fbe71SArnaud Ebalard * should accept followings: 862594fbe71SArnaud Ebalard * 863594fbe71SArnaud Ebalard * 0 : no fan speed control (i.e. fan at full speed) 864594fbe71SArnaud Ebalard * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop) 865594fbe71SArnaud Ebalard * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop) 866594fbe71SArnaud Ebalard * 867594fbe71SArnaud Ebalard * but we do not accept 0 as this mode is not natively supported by the chip 868594fbe71SArnaud Ebalard * and it is not emulated by g762 driver. -EINVAL is returned in this case. 869594fbe71SArnaud Ebalard */ 870594fbe71SArnaud Ebalard static ssize_t get_pwm_enable(struct device *dev, 871594fbe71SArnaud Ebalard struct device_attribute *da, char *buf) 872594fbe71SArnaud Ebalard { 873594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 874594fbe71SArnaud Ebalard 875594fbe71SArnaud Ebalard if (IS_ERR(data)) 876594fbe71SArnaud Ebalard return PTR_ERR(data); 877594fbe71SArnaud Ebalard 878594fbe71SArnaud Ebalard return sprintf(buf, "%d\n", 879594fbe71SArnaud Ebalard (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1); 880594fbe71SArnaud Ebalard } 881594fbe71SArnaud Ebalard 882594fbe71SArnaud Ebalard static ssize_t set_pwm_enable(struct device *dev, 883594fbe71SArnaud Ebalard struct device_attribute *da, 884594fbe71SArnaud Ebalard const char *buf, size_t count) 885594fbe71SArnaud Ebalard { 886594fbe71SArnaud Ebalard unsigned long val; 887594fbe71SArnaud Ebalard int ret; 888594fbe71SArnaud Ebalard 889594fbe71SArnaud Ebalard if (kstrtoul(buf, 10, &val)) 890594fbe71SArnaud Ebalard return -EINVAL; 891594fbe71SArnaud Ebalard 892594fbe71SArnaud Ebalard ret = do_set_pwm_enable(dev, val); 893594fbe71SArnaud Ebalard if (ret < 0) 894594fbe71SArnaud Ebalard return ret; 895594fbe71SArnaud Ebalard 896594fbe71SArnaud Ebalard return count; 897594fbe71SArnaud Ebalard } 898594fbe71SArnaud Ebalard 899594fbe71SArnaud Ebalard /* 900594fbe71SArnaud Ebalard * Read and write functions for pwm1 sysfs file. Get and set pwm value 901594fbe71SArnaud Ebalard * (which affects fan speed) in open-loop mode. 0 stops the fan and 255 902594fbe71SArnaud Ebalard * makes it run at full speed. 903594fbe71SArnaud Ebalard */ 904594fbe71SArnaud Ebalard static ssize_t get_pwm(struct device *dev, struct device_attribute *da, 905594fbe71SArnaud Ebalard char *buf) 906594fbe71SArnaud Ebalard { 907594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 908594fbe71SArnaud Ebalard 909594fbe71SArnaud Ebalard if (IS_ERR(data)) 910594fbe71SArnaud Ebalard return PTR_ERR(data); 911594fbe71SArnaud Ebalard 912594fbe71SArnaud Ebalard return sprintf(buf, "%d\n", data->set_out); 913594fbe71SArnaud Ebalard } 914594fbe71SArnaud Ebalard 915594fbe71SArnaud Ebalard static ssize_t set_pwm(struct device *dev, struct device_attribute *da, 916594fbe71SArnaud Ebalard const char *buf, size_t count) 917594fbe71SArnaud Ebalard { 918594fbe71SArnaud Ebalard unsigned long val; 919594fbe71SArnaud Ebalard int ret; 920594fbe71SArnaud Ebalard 921594fbe71SArnaud Ebalard if (kstrtoul(buf, 10, &val)) 922594fbe71SArnaud Ebalard return -EINVAL; 923594fbe71SArnaud Ebalard 924594fbe71SArnaud Ebalard ret = do_set_pwm(dev, val); 925594fbe71SArnaud Ebalard if (ret < 0) 926594fbe71SArnaud Ebalard return ret; 927594fbe71SArnaud Ebalard 928594fbe71SArnaud Ebalard return count; 929594fbe71SArnaud Ebalard } 930594fbe71SArnaud Ebalard 931594fbe71SArnaud Ebalard /* 932594fbe71SArnaud Ebalard * Read and write function for fan1_target sysfs file. Get/set the fan speed in 933594fbe71SArnaud Ebalard * closed-loop mode. Speed is given as a RPM value; then the chip will regulate 934594fbe71SArnaud Ebalard * the fan speed using pulses from fan tachometer. 935594fbe71SArnaud Ebalard * 936594fbe71SArnaud Ebalard * Refer to rpm_from_cnt() implementation above to get info about count number 937594fbe71SArnaud Ebalard * calculation. 938594fbe71SArnaud Ebalard * 939594fbe71SArnaud Ebalard * Also note that due to rounding errors it is possible that you don't read 940594fbe71SArnaud Ebalard * back exactly the value you have set. 941594fbe71SArnaud Ebalard */ 942594fbe71SArnaud Ebalard static ssize_t get_fan_target(struct device *dev, struct device_attribute *da, 943594fbe71SArnaud Ebalard char *buf) 944594fbe71SArnaud Ebalard { 945594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 946594fbe71SArnaud Ebalard unsigned int rpm; 947594fbe71SArnaud Ebalard 948594fbe71SArnaud Ebalard if (IS_ERR(data)) 949594fbe71SArnaud Ebalard return PTR_ERR(data); 950594fbe71SArnaud Ebalard 951594fbe71SArnaud Ebalard mutex_lock(&data->update_lock); 952594fbe71SArnaud Ebalard rpm = rpm_from_cnt(data->set_cnt, data->clk_freq, 953594fbe71SArnaud Ebalard G762_PULSE_FROM_REG(data->fan_cmd1), 954594fbe71SArnaud Ebalard G762_CLKDIV_FROM_REG(data->fan_cmd1), 955594fbe71SArnaud Ebalard G762_GEARMULT_FROM_REG(data->fan_cmd2)); 956594fbe71SArnaud Ebalard mutex_unlock(&data->update_lock); 957594fbe71SArnaud Ebalard 958594fbe71SArnaud Ebalard return sprintf(buf, "%u\n", rpm); 959594fbe71SArnaud Ebalard } 960594fbe71SArnaud Ebalard 961594fbe71SArnaud Ebalard static ssize_t set_fan_target(struct device *dev, struct device_attribute *da, 962594fbe71SArnaud Ebalard const char *buf, size_t count) 963594fbe71SArnaud Ebalard { 964594fbe71SArnaud Ebalard unsigned long val; 965594fbe71SArnaud Ebalard int ret; 966594fbe71SArnaud Ebalard 967594fbe71SArnaud Ebalard if (kstrtoul(buf, 10, &val)) 968594fbe71SArnaud Ebalard return -EINVAL; 969594fbe71SArnaud Ebalard 970594fbe71SArnaud Ebalard ret = do_set_fan_target(dev, val); 971594fbe71SArnaud Ebalard if (ret < 0) 972594fbe71SArnaud Ebalard return ret; 973594fbe71SArnaud Ebalard 974594fbe71SArnaud Ebalard return count; 975594fbe71SArnaud Ebalard } 976594fbe71SArnaud Ebalard 977594fbe71SArnaud Ebalard /* read function for fan1_fault sysfs file. */ 978594fbe71SArnaud Ebalard static ssize_t get_fan_failure(struct device *dev, struct device_attribute *da, 979594fbe71SArnaud Ebalard char *buf) 980594fbe71SArnaud Ebalard { 981594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 982594fbe71SArnaud Ebalard 983594fbe71SArnaud Ebalard if (IS_ERR(data)) 984594fbe71SArnaud Ebalard return PTR_ERR(data); 985594fbe71SArnaud Ebalard 986594fbe71SArnaud Ebalard return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL)); 987594fbe71SArnaud Ebalard } 988594fbe71SArnaud Ebalard 989594fbe71SArnaud Ebalard /* 990594fbe71SArnaud Ebalard * read function for fan1_alarm sysfs file. Note that OOC condition is 991594fbe71SArnaud Ebalard * enabled low 992594fbe71SArnaud Ebalard */ 993594fbe71SArnaud Ebalard static ssize_t get_fan_ooc(struct device *dev, struct device_attribute *da, 994594fbe71SArnaud Ebalard char *buf) 995594fbe71SArnaud Ebalard { 996594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 997594fbe71SArnaud Ebalard 998594fbe71SArnaud Ebalard if (IS_ERR(data)) 999594fbe71SArnaud Ebalard return PTR_ERR(data); 1000594fbe71SArnaud Ebalard 1001594fbe71SArnaud Ebalard return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC)); 1002594fbe71SArnaud Ebalard } 1003594fbe71SArnaud Ebalard 1004594fbe71SArnaud Ebalard static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm); 1005594fbe71SArnaud Ebalard static DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, get_pwm_mode, set_pwm_mode); 1006594fbe71SArnaud Ebalard static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, 1007594fbe71SArnaud Ebalard get_pwm_enable, set_pwm_enable); 1008594fbe71SArnaud Ebalard static DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL); 1009594fbe71SArnaud Ebalard static DEVICE_ATTR(fan1_alarm, S_IRUGO, get_fan_ooc, NULL); 1010594fbe71SArnaud Ebalard static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL); 1011594fbe71SArnaud Ebalard static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, 1012594fbe71SArnaud Ebalard get_fan_target, set_fan_target); 1013594fbe71SArnaud Ebalard static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div); 1014594fbe71SArnaud Ebalard static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, 1015594fbe71SArnaud Ebalard get_fan_pulses, set_fan_pulses); 1016594fbe71SArnaud Ebalard 1017594fbe71SArnaud Ebalard /* Driver data */ 1018398e16dbSAxel Lin static struct attribute *g762_attrs[] = { 1019594fbe71SArnaud Ebalard &dev_attr_fan1_input.attr, 1020594fbe71SArnaud Ebalard &dev_attr_fan1_alarm.attr, 1021594fbe71SArnaud Ebalard &dev_attr_fan1_fault.attr, 1022594fbe71SArnaud Ebalard &dev_attr_fan1_target.attr, 1023594fbe71SArnaud Ebalard &dev_attr_fan1_div.attr, 1024594fbe71SArnaud Ebalard &dev_attr_fan1_pulses.attr, 1025594fbe71SArnaud Ebalard &dev_attr_pwm1.attr, 1026594fbe71SArnaud Ebalard &dev_attr_pwm1_mode.attr, 1027594fbe71SArnaud Ebalard &dev_attr_pwm1_enable.attr, 1028594fbe71SArnaud Ebalard NULL 1029594fbe71SArnaud Ebalard }; 1030594fbe71SArnaud Ebalard 1031398e16dbSAxel Lin ATTRIBUTE_GROUPS(g762); 1032594fbe71SArnaud Ebalard 1033594fbe71SArnaud Ebalard /* 1034594fbe71SArnaud Ebalard * Enable both fan failure detection and fan out of control protection. The 1035594fbe71SArnaud Ebalard * function does not protect change/access to data structure; it must thus 1036594fbe71SArnaud Ebalard * only be called during initialization. 1037594fbe71SArnaud Ebalard */ 1038594fbe71SArnaud Ebalard static inline int g762_fan_init(struct device *dev) 1039594fbe71SArnaud Ebalard { 1040594fbe71SArnaud Ebalard struct g762_data *data = g762_update_client(dev); 1041594fbe71SArnaud Ebalard 1042594fbe71SArnaud Ebalard if (IS_ERR(data)) 1043594fbe71SArnaud Ebalard return PTR_ERR(data); 1044594fbe71SArnaud Ebalard 1045594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL; 1046594fbe71SArnaud Ebalard data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC; 1047594fbe71SArnaud Ebalard data->valid = false; 1048594fbe71SArnaud Ebalard 1049398e16dbSAxel Lin return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, 1050594fbe71SArnaud Ebalard data->fan_cmd1); 1051594fbe71SArnaud Ebalard } 1052594fbe71SArnaud Ebalard 1053594fbe71SArnaud Ebalard static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) 1054594fbe71SArnaud Ebalard { 1055398e16dbSAxel Lin struct device *dev = &client->dev; 1056594fbe71SArnaud Ebalard struct g762_data *data; 1057594fbe71SArnaud Ebalard int ret; 1058594fbe71SArnaud Ebalard 1059594fbe71SArnaud Ebalard if (!i2c_check_functionality(client->adapter, 1060594fbe71SArnaud Ebalard I2C_FUNC_SMBUS_BYTE_DATA)) 1061594fbe71SArnaud Ebalard return -ENODEV; 1062594fbe71SArnaud Ebalard 1063398e16dbSAxel Lin data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL); 1064594fbe71SArnaud Ebalard if (!data) 1065594fbe71SArnaud Ebalard return -ENOMEM; 1066594fbe71SArnaud Ebalard 1067594fbe71SArnaud Ebalard i2c_set_clientdata(client, data); 1068594fbe71SArnaud Ebalard data->client = client; 1069594fbe71SArnaud Ebalard mutex_init(&data->update_lock); 1070594fbe71SArnaud Ebalard 1071594fbe71SArnaud Ebalard /* Enable fan failure detection and fan out of control protection */ 1072398e16dbSAxel Lin ret = g762_fan_init(dev); 1073594fbe71SArnaud Ebalard if (ret) 1074594fbe71SArnaud Ebalard return ret; 1075594fbe71SArnaud Ebalard 1076594fbe71SArnaud Ebalard /* Get configuration via DT ... */ 1077594fbe71SArnaud Ebalard ret = g762_of_clock_enable(client); 1078594fbe71SArnaud Ebalard if (ret) 1079594fbe71SArnaud Ebalard return ret; 1080594fbe71SArnaud Ebalard ret = g762_of_prop_import(client); 1081594fbe71SArnaud Ebalard if (ret) 1082594fbe71SArnaud Ebalard goto clock_dis; 1083594fbe71SArnaud Ebalard /* ... or platform_data */ 1084594fbe71SArnaud Ebalard ret = g762_pdata_prop_import(client); 1085594fbe71SArnaud Ebalard if (ret) 1086594fbe71SArnaud Ebalard goto clock_dis; 1087594fbe71SArnaud Ebalard 10886b19b660SArnaud Ebalard data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name, 10896b19b660SArnaud Ebalard data, g762_groups); 1090594fbe71SArnaud Ebalard if (IS_ERR(data->hwmon_dev)) { 1091594fbe71SArnaud Ebalard ret = PTR_ERR(data->hwmon_dev); 1092398e16dbSAxel Lin goto clock_dis; 1093594fbe71SArnaud Ebalard } 1094594fbe71SArnaud Ebalard 1095594fbe71SArnaud Ebalard return 0; 1096594fbe71SArnaud Ebalard 1097594fbe71SArnaud Ebalard clock_dis: 1098594fbe71SArnaud Ebalard g762_of_clock_disable(client); 1099594fbe71SArnaud Ebalard 1100594fbe71SArnaud Ebalard return ret; 1101594fbe71SArnaud Ebalard } 1102594fbe71SArnaud Ebalard 1103594fbe71SArnaud Ebalard static int g762_remove(struct i2c_client *client) 1104594fbe71SArnaud Ebalard { 1105594fbe71SArnaud Ebalard struct g762_data *data = i2c_get_clientdata(client); 1106594fbe71SArnaud Ebalard 1107594fbe71SArnaud Ebalard hwmon_device_unregister(data->hwmon_dev); 1108594fbe71SArnaud Ebalard g762_of_clock_disable(client); 1109594fbe71SArnaud Ebalard 1110594fbe71SArnaud Ebalard return 0; 1111594fbe71SArnaud Ebalard } 1112594fbe71SArnaud Ebalard 1113594fbe71SArnaud Ebalard static struct i2c_driver g762_driver = { 1114594fbe71SArnaud Ebalard .driver = { 1115594fbe71SArnaud Ebalard .name = DRVNAME, 1116594fbe71SArnaud Ebalard .owner = THIS_MODULE, 1117594fbe71SArnaud Ebalard .of_match_table = of_match_ptr(g762_dt_match), 1118594fbe71SArnaud Ebalard }, 1119594fbe71SArnaud Ebalard .probe = g762_probe, 1120594fbe71SArnaud Ebalard .remove = g762_remove, 1121594fbe71SArnaud Ebalard .id_table = g762_id, 1122594fbe71SArnaud Ebalard }; 1123594fbe71SArnaud Ebalard 1124594fbe71SArnaud Ebalard module_i2c_driver(g762_driver); 1125594fbe71SArnaud Ebalard 1126594fbe71SArnaud Ebalard MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>"); 1127594fbe71SArnaud Ebalard MODULE_DESCRIPTION("GMT G762/G763 driver"); 1128594fbe71SArnaud Ebalard MODULE_LICENSE("GPL"); 1129