Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
   4 *        PWM controller chips from G762 family, i.e. G762 and G763
   5 *
   6 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
   7 *
   8 * This work is based on a basic version for 2.6.31 kernel developed
   9 * by Olivier Mouchet for LaCie. Updates and correction have been
  10 * performed to run on recent kernels. Additional features, like the
  11 * ability to configure various characteristics via .dts file or
  12 * board init file have been added. Detailed datasheet on which this
  13 * development is based is available here:
  14 *
  15 *  http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
  16 *
  17 * Headers from previous developments have been kept below:
  18 *
  19 * Copyright (c) 2009 LaCie
  20 *
  21 * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
  22 *
  23 * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
  24 * Copyright (C) 2007  Herbert Valerio Riedel <hvr@gnu.org>
  25 *
  26 * g762: minimal datasheet available at:
  27 *       http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  28 */
  29
  30#include <linux/device.h>
  31#include <linux/module.h>
  32#include <linux/init.h>
  33#include <linux/jiffies.h>
  34#include <linux/i2c.h>
  35#include <linux/hwmon.h>
  36#include <linux/hwmon-sysfs.h>
  37#include <linux/err.h>
  38#include <linux/mutex.h>
  39#include <linux/kernel.h>
  40#include <linux/clk.h>
  41#include <linux/of.h>
 
  42#include <linux/platform_data/g762.h>
  43
  44#define DRVNAME "g762"
  45
  46static const struct i2c_device_id g762_id[] = {
  47	{ "g762", 0 },
  48	{ "g763", 0 },
  49	{ }
  50};
  51MODULE_DEVICE_TABLE(i2c, g762_id);
  52
  53enum g762_regs {
  54	G762_REG_SET_CNT  = 0x00,
  55	G762_REG_ACT_CNT  = 0x01,
  56	G762_REG_FAN_STA  = 0x02,
  57	G762_REG_SET_OUT  = 0x03,
  58	G762_REG_FAN_CMD1 = 0x04,
  59	G762_REG_FAN_CMD2 = 0x05,
  60};
  61
  62/* Config register bits */
  63#define G762_REG_FAN_CMD1_DET_FAN_FAIL  0x80 /* enable fan_fail signal */
  64#define G762_REG_FAN_CMD1_DET_FAN_OOC   0x40 /* enable fan_out_of_control */
  65#define G762_REG_FAN_CMD1_OUT_MODE      0x20 /* out mode: PWM or DC */
  66#define G762_REG_FAN_CMD1_FAN_MODE      0x10 /* fan mode: closed/open-loop */
  67#define G762_REG_FAN_CMD1_CLK_DIV_ID1   0x08 /* clock divisor value */
  68#define G762_REG_FAN_CMD1_CLK_DIV_ID0   0x04
  69#define G762_REG_FAN_CMD1_PWM_POLARITY  0x02 /* PWM polarity */
  70#define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
  71
  72#define G762_REG_FAN_CMD2_GEAR_MODE_1   0x08 /* fan gear mode */
  73#define G762_REG_FAN_CMD2_GEAR_MODE_0   0x04
  74#define G762_REG_FAN_CMD2_FAN_STARTV_1  0x02 /* fan startup voltage */
  75#define G762_REG_FAN_CMD2_FAN_STARTV_0  0x01
  76
  77#define G762_REG_FAN_STA_FAIL           0x02 /* fan fail */
  78#define G762_REG_FAN_STA_OOC            0x01 /* fan out of control */
  79
  80/* Config register values */
  81#define G762_OUT_MODE_PWM            1
  82#define G762_OUT_MODE_DC             0
  83
  84#define G762_FAN_MODE_CLOSED_LOOP    2
  85#define G762_FAN_MODE_OPEN_LOOP      1
  86
  87#define G762_PWM_POLARITY_NEGATIVE   1
  88#define G762_PWM_POLARITY_POSITIVE   0
  89
  90/* Register data is read (and cached) at most once per second. */
  91#define G762_UPDATE_INTERVAL    HZ
  92
  93/*
  94 * Extract pulse count per fan revolution value (2 or 4) from given
  95 * FAN_CMD1 register value.
  96 */
  97#define G762_PULSE_FROM_REG(reg) \
  98	((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
  99
 100/*
 101 * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
 102 * register value.
 103 */
 104#define G762_CLKDIV_FROM_REG(reg) \
 105	(1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 |	\
 106			 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
 107
 108/*
 109 * Extract fan gear mode multiplier value (0, 2 or 4) from given
 110 * FAN_CMD2 register value.
 111 */
 112#define G762_GEARMULT_FROM_REG(reg) \
 113	(1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 |	\
 114			 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
 115
 116struct g762_data {
 117	struct i2c_client *client;
 118	struct clk *clk;
 119
 120	/* update mutex */
 121	struct mutex update_lock;
 122
 123	/* board specific parameters. */
 124	u32 clk_freq;
 125
 126	/* g762 register cache */
 127	bool valid;
 128	unsigned long last_updated; /* in jiffies */
 129
 130	u8 set_cnt;  /* controls fan rotation speed in closed-loop mode */
 131	u8 act_cnt;  /* provides access to current fan RPM value */
 132	u8 fan_sta;  /* bit 0: set when actual fan speed is more than
 133		      *        25% outside requested fan speed
 134		      * bit 1: set when no transition occurs on fan
 135		      *        pin for 0.7s
 136		      */
 137	u8 set_out;  /* controls fan rotation speed in open-loop mode */
 138	u8 fan_cmd1; /*   0: FG_PLS_ID0 FG pulses count per revolution
 139		      *      0: 2 counts per revolution
 140		      *      1: 4 counts per revolution
 141		      *   1: PWM_POLARITY 1: negative_duty
 142		      *                   0: positive_duty
 143		      * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
 144		      *         00: Divide fan clock by 1
 145		      *         01: Divide fan clock by 2
 146		      *         10: Divide fan clock by 4
 147		      *         11: Divide fan clock by 8
 148		      *   4: FAN_MODE 1:closed-loop, 0:open-loop
 149		      *   5: OUT_MODE 1:PWM, 0:DC
 150		      *   6: DET_FAN_OOC enable "fan ooc" status
 151		      *   7: DET_FAN_FAIL enable "fan fail" status
 152		      */
 153	u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
 154		      * 2,3: FG_GEAR_MODE
 155		      *         00: multiplier = 1
 156		      *         01: multiplier = 2
 157		      *         10: multiplier = 4
 158		      *   4: Mask ALERT# (g763 only)
 159		      */
 160};
 161
 162/*
 163 * Convert count value from fan controller register (FAN_SET_CNT) into fan
 164 * speed RPM value. Note that the datasheet documents a basic formula;
 165 * influence of additional parameters (fan clock divisor, fan gear mode)
 166 * have been infered from examples in the datasheet and tests.
 167 */
 168static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
 169					u8 clk_div, u8 gear_mult)
 170{
 171	if (cnt == 0xff)  /* setting cnt to 255 stops the fan */
 172		return 0;
 173
 174	return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
 175}
 176
 177/*
 178 * Convert fan RPM value from sysfs into count value for fan controller
 179 * register (FAN_SET_CNT).
 180 */
 181static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
 182					 u8 clk_div, u8 gear_mult)
 183{
 184	unsigned long f1 = clk_freq * 30 * gear_mult;
 185	unsigned long f2 = p * clk_div;
 186
 187	if (!rpm)	/* to stop the fan, set cnt to 255 */
 188		return 0xff;
 189
 190	rpm = clamp_val(rpm, f1 / (255 * f2), ULONG_MAX / f2);
 191	return DIV_ROUND_CLOSEST(f1, rpm * f2);
 192}
 193
 194/* helper to grab and cache data, at most one time per second */
 195static struct g762_data *g762_update_client(struct device *dev)
 196{
 197	struct g762_data *data = dev_get_drvdata(dev);
 198	struct i2c_client *client = data->client;
 199	int ret = 0;
 200
 201	mutex_lock(&data->update_lock);
 202	if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
 203	    likely(data->valid))
 204		goto out;
 205
 206	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
 207	if (ret < 0)
 208		goto out;
 209	data->set_cnt = ret;
 210
 211	ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
 212	if (ret < 0)
 213		goto out;
 214	data->act_cnt = ret;
 215
 216	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
 217	if (ret < 0)
 218		goto out;
 219	data->fan_sta = ret;
 220
 221	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
 222	if (ret < 0)
 223		goto out;
 224	data->set_out = ret;
 225
 226	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
 227	if (ret < 0)
 228		goto out;
 229	data->fan_cmd1 = ret;
 230
 231	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
 232	if (ret < 0)
 233		goto out;
 234	data->fan_cmd2 = ret;
 235
 236	data->last_updated = jiffies;
 237	data->valid = true;
 238 out:
 239	mutex_unlock(&data->update_lock);
 240
 241	if (ret < 0) /* upon error, encode it in return value */
 242		data = ERR_PTR(ret);
 243
 244	return data;
 245}
 246
 247/* helpers for writing hardware parameters */
 248
 249/*
 250 * Set input clock frequency received on CLK pin of the chip. Accepted values
 251 * are between 0 and 0xffffff. If zero is given, then default frequency
 252 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
 253 * system but an internal parameter, i.e. value is not passed to the device.
 254 */
 255static int do_set_clk_freq(struct device *dev, unsigned long val)
 256{
 257	struct g762_data *data = dev_get_drvdata(dev);
 258
 259	if (val > 0xffffff)
 260		return -EINVAL;
 261	if (!val)
 262		val = 32768;
 263
 264	data->clk_freq = val;
 265
 266	return 0;
 267}
 268
 269/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
 270static int do_set_pwm_mode(struct device *dev, unsigned long val)
 271{
 272	struct g762_data *data = g762_update_client(dev);
 273	int ret;
 274
 275	if (IS_ERR(data))
 276		return PTR_ERR(data);
 277
 278	mutex_lock(&data->update_lock);
 279	switch (val) {
 280	case G762_OUT_MODE_PWM:
 281		data->fan_cmd1 |=  G762_REG_FAN_CMD1_OUT_MODE;
 282		break;
 283	case G762_OUT_MODE_DC:
 284		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
 285		break;
 286	default:
 287		ret = -EINVAL;
 288		goto out;
 289	}
 290	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 291					data->fan_cmd1);
 292	data->valid = false;
 293 out:
 294	mutex_unlock(&data->update_lock);
 295
 296	return ret;
 297}
 298
 299/* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
 300static int do_set_fan_div(struct device *dev, unsigned long val)
 301{
 302	struct g762_data *data = g762_update_client(dev);
 303	int ret;
 304
 305	if (IS_ERR(data))
 306		return PTR_ERR(data);
 307
 308	mutex_lock(&data->update_lock);
 309	switch (val) {
 310	case 1:
 311		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
 312		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
 313		break;
 314	case 2:
 315		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
 316		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
 317		break;
 318	case 4:
 319		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
 320		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
 321		break;
 322	case 8:
 323		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
 324		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
 325		break;
 326	default:
 327		ret = -EINVAL;
 328		goto out;
 329	}
 330	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 331					data->fan_cmd1);
 332	data->valid = false;
 333 out:
 334	mutex_unlock(&data->update_lock);
 335
 336	return ret;
 337}
 338
 339/* Set fan gear mode. Accepts either 0, 1 or 2. */
 340static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
 341{
 342	struct g762_data *data = g762_update_client(dev);
 343	int ret;
 344
 345	if (IS_ERR(data))
 346		return PTR_ERR(data);
 347
 348	mutex_lock(&data->update_lock);
 349	switch (val) {
 350	case 0:
 351		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
 352		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
 353		break;
 354	case 1:
 355		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_0;
 356		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
 357		break;
 358	case 2:
 359		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
 360		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_1;
 361		break;
 362	default:
 363		ret = -EINVAL;
 364		goto out;
 365	}
 366	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
 367					data->fan_cmd2);
 368	data->valid = false;
 369 out:
 370	mutex_unlock(&data->update_lock);
 371
 372	return ret;
 373}
 374
 375/* Set number of fan pulses per revolution. Accepts either 2 or 4. */
 376static int do_set_fan_pulses(struct device *dev, unsigned long val)
 377{
 378	struct g762_data *data = g762_update_client(dev);
 379	int ret;
 380
 381	if (IS_ERR(data))
 382		return PTR_ERR(data);
 383
 384	mutex_lock(&data->update_lock);
 385	switch (val) {
 386	case 2:
 387		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
 388		break;
 389	case 4:
 390		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PULSE_PER_REV;
 391		break;
 392	default:
 393		ret = -EINVAL;
 394		goto out;
 395	}
 396	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 397					data->fan_cmd1);
 398	data->valid = false;
 399 out:
 400	mutex_unlock(&data->update_lock);
 401
 402	return ret;
 403}
 404
 405/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
 406static int do_set_pwm_enable(struct device *dev, unsigned long val)
 407{
 408	struct g762_data *data = g762_update_client(dev);
 409	int ret;
 410
 411	if (IS_ERR(data))
 412		return PTR_ERR(data);
 413
 414	mutex_lock(&data->update_lock);
 415	switch (val) {
 416	case G762_FAN_MODE_CLOSED_LOOP:
 417		data->fan_cmd1 |=  G762_REG_FAN_CMD1_FAN_MODE;
 418		break;
 419	case G762_FAN_MODE_OPEN_LOOP:
 420		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
 421		/*
 422		 * BUG FIX: if SET_CNT register value is 255 then, for some
 423		 * unknown reason, fan will not rotate as expected, no matter
 424		 * the value of SET_OUT (to be specific, this seems to happen
 425		 * only in PWM mode). To workaround this bug, we give SET_CNT
 426		 * value of 254 if it is 255 when switching to open-loop.
 427		 */
 428		if (data->set_cnt == 0xff)
 429			i2c_smbus_write_byte_data(data->client,
 430						  G762_REG_SET_CNT, 254);
 431		break;
 432	default:
 433		ret = -EINVAL;
 434		goto out;
 435	}
 436
 437	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 438					data->fan_cmd1);
 439	data->valid = false;
 440 out:
 441	mutex_unlock(&data->update_lock);
 442
 443	return ret;
 444}
 445
 446/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
 447static int do_set_pwm_polarity(struct device *dev, unsigned long val)
 448{
 449	struct g762_data *data = g762_update_client(dev);
 450	int ret;
 451
 452	if (IS_ERR(data))
 453		return PTR_ERR(data);
 454
 455	mutex_lock(&data->update_lock);
 456	switch (val) {
 457	case G762_PWM_POLARITY_POSITIVE:
 458		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
 459		break;
 460	case G762_PWM_POLARITY_NEGATIVE:
 461		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PWM_POLARITY;
 462		break;
 463	default:
 464		ret = -EINVAL;
 465		goto out;
 466	}
 467	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 468					data->fan_cmd1);
 469	data->valid = false;
 470 out:
 471	mutex_unlock(&data->update_lock);
 472
 473	return ret;
 474}
 475
 476/*
 477 * Set pwm value. Accepts values between 0 (stops the fan) and
 478 * 255 (full speed). This only makes sense in open-loop mode.
 479 */
 480static int do_set_pwm(struct device *dev, unsigned long val)
 481{
 482	struct g762_data *data = dev_get_drvdata(dev);
 483	struct i2c_client *client = data->client;
 484	int ret;
 485
 486	if (val > 255)
 487		return -EINVAL;
 488
 489	mutex_lock(&data->update_lock);
 490	ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
 491	data->valid = false;
 492	mutex_unlock(&data->update_lock);
 493
 494	return ret;
 495}
 496
 497/*
 498 * Set fan RPM value. Can be called both in closed and open-loop mode
 499 * but effect will only be seen after closed-loop mode is configured.
 500 */
 501static int do_set_fan_target(struct device *dev, unsigned long val)
 502{
 503	struct g762_data *data = g762_update_client(dev);
 504	int ret;
 505
 506	if (IS_ERR(data))
 507		return PTR_ERR(data);
 508
 509	mutex_lock(&data->update_lock);
 510	data->set_cnt = cnt_from_rpm(val, data->clk_freq,
 511				     G762_PULSE_FROM_REG(data->fan_cmd1),
 512				     G762_CLKDIV_FROM_REG(data->fan_cmd1),
 513				     G762_GEARMULT_FROM_REG(data->fan_cmd2));
 514	ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
 515					data->set_cnt);
 516	data->valid = false;
 517	mutex_unlock(&data->update_lock);
 518
 519	return ret;
 520}
 521
 522/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
 523static int do_set_fan_startv(struct device *dev, unsigned long val)
 524{
 525	struct g762_data *data = g762_update_client(dev);
 526	int ret;
 527
 528	if (IS_ERR(data))
 529		return PTR_ERR(data);
 530
 531	mutex_lock(&data->update_lock);
 532	switch (val) {
 533	case 0:
 534		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
 535		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
 536		break;
 537	case 1:
 538		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
 539		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
 540		break;
 541	case 2:
 542		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
 543		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
 544		break;
 545	case 3:
 546		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
 547		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
 548		break;
 549	default:
 550		ret = -EINVAL;
 551		goto out;
 552	}
 553	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
 554					data->fan_cmd2);
 555	data->valid = false;
 556 out:
 557	mutex_unlock(&data->update_lock);
 558
 559	return ret;
 560}
 561
 562/*
 563 * Helper to import hardware characteristics from .dts file and push
 564 * those to the chip.
 565 */
 566
 567#ifdef CONFIG_OF
 568static const struct of_device_id g762_dt_match[] = {
 569	{ .compatible = "gmt,g762" },
 570	{ .compatible = "gmt,g763" },
 571	{ },
 572};
 573MODULE_DEVICE_TABLE(of, g762_dt_match);
 574
 575/*
 576 * Grab clock (a required property), enable it, get (fixed) clock frequency
 577 * and store it. Note: upon success, clock has been prepared and enabled; it
 578 * must later be unprepared and disabled (e.g. during module unloading) by a
 579 * call to g762_of_clock_disable(). Note that a reference to clock is kept
 580 * in our private data structure to be used in this function.
 581 */
 582static void g762_of_clock_disable(void *data)
 583{
 584	struct g762_data *g762 = data;
 585
 586	clk_disable_unprepare(g762->clk);
 587	clk_put(g762->clk);
 588}
 589
 590static int g762_of_clock_enable(struct i2c_client *client)
 591{
 592	struct g762_data *data;
 593	unsigned long clk_freq;
 594	struct clk *clk;
 595	int ret;
 596
 597	if (!client->dev.of_node)
 598		return 0;
 599
 600	clk = of_clk_get(client->dev.of_node, 0);
 601	if (IS_ERR(clk)) {
 602		dev_err(&client->dev, "failed to get clock\n");
 603		return PTR_ERR(clk);
 604	}
 605
 606	ret = clk_prepare_enable(clk);
 607	if (ret) {
 608		dev_err(&client->dev, "failed to enable clock\n");
 609		goto clk_put;
 610	}
 611
 612	clk_freq = clk_get_rate(clk);
 613	ret = do_set_clk_freq(&client->dev, clk_freq);
 614	if (ret) {
 615		dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
 616		goto clk_unprep;
 617	}
 618
 619	data = i2c_get_clientdata(client);
 620	data->clk = clk;
 621
 622	ret = devm_add_action(&client->dev, g762_of_clock_disable, data);
 623	if (ret) {
 624		dev_err(&client->dev, "failed to add disable clock action\n");
 625		goto clk_unprep;
 626	}
 627
 628	return 0;
 629
 630 clk_unprep:
 631	clk_disable_unprepare(clk);
 632
 633 clk_put:
 634	clk_put(clk);
 635
 636	return ret;
 637}
 638
 639static int g762_of_prop_import_one(struct i2c_client *client,
 640				   const char *pname,
 641				   int (*psetter)(struct device *dev,
 642						  unsigned long val))
 643{
 644	int ret;
 645	u32 pval;
 646
 647	if (of_property_read_u32(client->dev.of_node, pname, &pval))
 648		return 0;
 649
 650	dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
 651	ret = (*psetter)(&client->dev, pval);
 652	if (ret)
 653		dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
 654
 655	return ret;
 656}
 657
 658static int g762_of_prop_import(struct i2c_client *client)
 659{
 660	int ret;
 661
 662	if (!client->dev.of_node)
 663		return 0;
 664
 665	ret = g762_of_prop_import_one(client, "fan_gear_mode",
 666				      do_set_fan_gear_mode);
 667	if (ret)
 668		return ret;
 669
 670	ret = g762_of_prop_import_one(client, "pwm_polarity",
 671				      do_set_pwm_polarity);
 672	if (ret)
 673		return ret;
 674
 675	return g762_of_prop_import_one(client, "fan_startv",
 676				       do_set_fan_startv);
 677}
 678
 679#else
 680static int g762_of_prop_import(struct i2c_client *client)
 681{
 682	return 0;
 683}
 684
 685static int g762_of_clock_enable(struct i2c_client *client)
 686{
 687	return 0;
 688}
 689#endif
 690
 691/*
 692 * Helper to import hardware characteristics from .dts file and push
 693 * those to the chip.
 694 */
 695
 696static int g762_pdata_prop_import(struct i2c_client *client)
 697{
 698	struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
 699	int ret;
 700
 701	if (!pdata)
 702		return 0;
 703
 704	ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
 705	if (ret)
 706		return ret;
 707
 708	ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
 709	if (ret)
 710		return ret;
 711
 712	ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
 713	if (ret)
 714		return ret;
 715
 716	return do_set_clk_freq(&client->dev, pdata->clk_freq);
 717}
 718
 719/*
 720 * sysfs attributes
 721 */
 722
 723/*
 724 * Read function for fan1_input sysfs file. Return current fan RPM value, or
 725 * 0 if fan is out of control.
 726 */
 727static ssize_t fan1_input_show(struct device *dev,
 728			       struct device_attribute *da, char *buf)
 729{
 730	struct g762_data *data = g762_update_client(dev);
 731	unsigned int rpm = 0;
 732
 733	if (IS_ERR(data))
 734		return PTR_ERR(data);
 735
 736	mutex_lock(&data->update_lock);
 737	/* reverse logic: fan out of control reporting is enabled low */
 738	if (data->fan_sta & G762_REG_FAN_STA_OOC) {
 739		rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
 740				   G762_PULSE_FROM_REG(data->fan_cmd1),
 741				   G762_CLKDIV_FROM_REG(data->fan_cmd1),
 742				   G762_GEARMULT_FROM_REG(data->fan_cmd2));
 743	}
 744	mutex_unlock(&data->update_lock);
 745
 746	return sprintf(buf, "%u\n", rpm);
 747}
 748
 749/*
 750 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
 751 * control mode i.e. PWM (1) or DC (0).
 752 */
 753static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da,
 754			      char *buf)
 755{
 756	struct g762_data *data = g762_update_client(dev);
 757
 758	if (IS_ERR(data))
 759		return PTR_ERR(data);
 760
 761	return sprintf(buf, "%d\n",
 762		       !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
 763}
 764
 765static ssize_t pwm1_mode_store(struct device *dev,
 766			       struct device_attribute *da, const char *buf,
 767			       size_t count)
 768{
 769	unsigned long val;
 770	int ret;
 771
 772	if (kstrtoul(buf, 10, &val))
 773		return -EINVAL;
 774
 775	ret = do_set_pwm_mode(dev, val);
 776	if (ret < 0)
 777		return ret;
 778
 779	return count;
 780}
 781
 782/*
 783 * Read and write functions for fan1_div sysfs file. Get and set fan
 784 * controller prescaler value
 785 */
 786static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
 787			     char *buf)
 788{
 789	struct g762_data *data = g762_update_client(dev);
 790
 791	if (IS_ERR(data))
 792		return PTR_ERR(data);
 793
 794	return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
 795}
 796
 797static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
 798			      const char *buf, size_t count)
 799{
 800	unsigned long val;
 801	int ret;
 802
 803	if (kstrtoul(buf, 10, &val))
 804		return -EINVAL;
 805
 806	ret = do_set_fan_div(dev, val);
 807	if (ret < 0)
 808		return ret;
 809
 810	return count;
 811}
 812
 813/*
 814 * Read and write functions for fan1_pulses sysfs file. Get and set number
 815 * of tachometer pulses per fan revolution.
 816 */
 817static ssize_t fan1_pulses_show(struct device *dev,
 818				struct device_attribute *da, char *buf)
 819{
 820	struct g762_data *data = g762_update_client(dev);
 821
 822	if (IS_ERR(data))
 823		return PTR_ERR(data);
 824
 825	return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
 826}
 827
 828static ssize_t fan1_pulses_store(struct device *dev,
 829				 struct device_attribute *da, const char *buf,
 830				 size_t count)
 831{
 832	unsigned long val;
 833	int ret;
 834
 835	if (kstrtoul(buf, 10, &val))
 836		return -EINVAL;
 837
 838	ret = do_set_fan_pulses(dev, val);
 839	if (ret < 0)
 840		return ret;
 841
 842	return count;
 843}
 844
 845/*
 846 * Read and write functions for pwm1_enable. Get and set fan speed control mode
 847 * (i.e. closed or open-loop).
 848 *
 849 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
 850 * should accept the following:
 851 *
 852 *  0 : no fan speed control (i.e. fan at full speed)
 853 *  1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
 854 *  2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
 855 *
 856 * but we do not accept 0 as this mode is not natively supported by the chip
 857 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
 858 */
 859static ssize_t pwm1_enable_show(struct device *dev,
 860				struct device_attribute *da, char *buf)
 861{
 862	struct g762_data *data = g762_update_client(dev);
 863
 864	if (IS_ERR(data))
 865		return PTR_ERR(data);
 866
 867	return sprintf(buf, "%d\n",
 868		       (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
 869}
 870
 871static ssize_t pwm1_enable_store(struct device *dev,
 872				 struct device_attribute *da, const char *buf,
 873				 size_t count)
 874{
 875	unsigned long val;
 876	int ret;
 877
 878	if (kstrtoul(buf, 10, &val))
 879		return -EINVAL;
 880
 881	ret = do_set_pwm_enable(dev, val);
 882	if (ret < 0)
 883		return ret;
 884
 885	return count;
 886}
 887
 888/*
 889 * Read and write functions for pwm1 sysfs file. Get and set pwm value
 890 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
 891 * makes it run at full speed.
 892 */
 893static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
 894			 char *buf)
 895{
 896	struct g762_data *data = g762_update_client(dev);
 897
 898	if (IS_ERR(data))
 899		return PTR_ERR(data);
 900
 901	return sprintf(buf, "%d\n", data->set_out);
 902}
 903
 904static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
 905			  const char *buf, size_t count)
 906{
 907	unsigned long val;
 908	int ret;
 909
 910	if (kstrtoul(buf, 10, &val))
 911		return -EINVAL;
 912
 913	ret = do_set_pwm(dev, val);
 914	if (ret < 0)
 915		return ret;
 916
 917	return count;
 918}
 919
 920/*
 921 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
 922 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
 923 * the fan speed using pulses from fan tachometer.
 924 *
 925 * Refer to rpm_from_cnt() implementation above to get info about count number
 926 * calculation.
 927 *
 928 * Also note that due to rounding errors it is possible that you don't read
 929 * back exactly the value you have set.
 930 */
 931static ssize_t fan1_target_show(struct device *dev,
 932				struct device_attribute *da, char *buf)
 933{
 934	struct g762_data *data = g762_update_client(dev);
 935	unsigned int rpm;
 936
 937	if (IS_ERR(data))
 938		return PTR_ERR(data);
 939
 940	mutex_lock(&data->update_lock);
 941	rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
 942			   G762_PULSE_FROM_REG(data->fan_cmd1),
 943			   G762_CLKDIV_FROM_REG(data->fan_cmd1),
 944			   G762_GEARMULT_FROM_REG(data->fan_cmd2));
 945	mutex_unlock(&data->update_lock);
 946
 947	return sprintf(buf, "%u\n", rpm);
 948}
 949
 950static ssize_t fan1_target_store(struct device *dev,
 951				 struct device_attribute *da, const char *buf,
 952				 size_t count)
 953{
 954	unsigned long val;
 955	int ret;
 956
 957	if (kstrtoul(buf, 10, &val))
 958		return -EINVAL;
 959
 960	ret = do_set_fan_target(dev, val);
 961	if (ret < 0)
 962		return ret;
 963
 964	return count;
 965}
 966
 967/* read function for fan1_fault sysfs file. */
 968static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da,
 969			       char *buf)
 970{
 971	struct g762_data *data = g762_update_client(dev);
 972
 973	if (IS_ERR(data))
 974		return PTR_ERR(data);
 975
 976	return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
 977}
 978
 979/*
 980 * read function for fan1_alarm sysfs file. Note that OOC condition is
 981 * enabled low
 982 */
 983static ssize_t fan1_alarm_show(struct device *dev,
 984			       struct device_attribute *da, char *buf)
 985{
 986	struct g762_data *data = g762_update_client(dev);
 987
 988	if (IS_ERR(data))
 989		return PTR_ERR(data);
 990
 991	return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
 992}
 993
 994static DEVICE_ATTR_RW(pwm1);
 995static DEVICE_ATTR_RW(pwm1_mode);
 996static DEVICE_ATTR_RW(pwm1_enable);
 997static DEVICE_ATTR_RO(fan1_input);
 998static DEVICE_ATTR_RO(fan1_alarm);
 999static DEVICE_ATTR_RO(fan1_fault);
1000static DEVICE_ATTR_RW(fan1_target);
1001static DEVICE_ATTR_RW(fan1_div);
1002static DEVICE_ATTR_RW(fan1_pulses);
1003
1004/* Driver data */
1005static struct attribute *g762_attrs[] = {
1006	&dev_attr_fan1_input.attr,
1007	&dev_attr_fan1_alarm.attr,
1008	&dev_attr_fan1_fault.attr,
1009	&dev_attr_fan1_target.attr,
1010	&dev_attr_fan1_div.attr,
1011	&dev_attr_fan1_pulses.attr,
1012	&dev_attr_pwm1.attr,
1013	&dev_attr_pwm1_mode.attr,
1014	&dev_attr_pwm1_enable.attr,
1015	NULL
1016};
1017
1018ATTRIBUTE_GROUPS(g762);
1019
1020/*
1021 * Enable both fan failure detection and fan out of control protection. The
1022 * function does not protect change/access to data structure; it must thus
1023 * only be called during initialization.
1024 */
1025static inline int g762_fan_init(struct device *dev)
1026{
1027	struct g762_data *data = g762_update_client(dev);
1028
1029	if (IS_ERR(data))
1030		return PTR_ERR(data);
1031
1032	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1033	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1034	data->valid = false;
1035
1036	return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
1037					 data->fan_cmd1);
1038}
1039
1040static int g762_probe(struct i2c_client *client)
1041{
1042	struct device *dev = &client->dev;
1043	struct device *hwmon_dev;
1044	struct g762_data *data;
1045	int ret;
1046
1047	if (!i2c_check_functionality(client->adapter,
1048				     I2C_FUNC_SMBUS_BYTE_DATA))
1049		return -ENODEV;
1050
1051	data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
1052	if (!data)
1053		return -ENOMEM;
1054
1055	i2c_set_clientdata(client, data);
1056	data->client = client;
1057	mutex_init(&data->update_lock);
1058
1059	/* Enable fan failure detection and fan out of control protection */
1060	ret = g762_fan_init(dev);
1061	if (ret)
1062		return ret;
1063
1064	/* Get configuration via DT ... */
1065	ret = g762_of_clock_enable(client);
1066	if (ret)
1067		return ret;
1068	ret = g762_of_prop_import(client);
1069	if (ret)
1070		return ret;
1071	/* ... or platform_data */
1072	ret = g762_pdata_prop_import(client);
1073	if (ret)
1074		return ret;
1075
1076	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1077							    data, g762_groups);
1078	return PTR_ERR_OR_ZERO(hwmon_dev);
1079}
1080
1081static struct i2c_driver g762_driver = {
1082	.driver = {
1083		.name = DRVNAME,
1084		.of_match_table = of_match_ptr(g762_dt_match),
1085	},
1086	.probe = g762_probe,
1087	.id_table = g762_id,
1088};
1089
1090module_i2c_driver(g762_driver);
1091
1092MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1093MODULE_DESCRIPTION("GMT G762/G763 driver");
1094MODULE_LICENSE("GPL");
v4.17
 
   1/*
   2 * g762 - Driver for the Global Mixed-mode Technology Inc. fan speed
   3 *        PWM controller chips from G762 family, i.e. G762 and G763
   4 *
   5 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
   6 *
   7 * This work is based on a basic version for 2.6.31 kernel developed
   8 * by Olivier Mouchet for LaCie. Updates and correction have been
   9 * performed to run on recent kernels. Additional features, like the
  10 * ability to configure various characteristics via .dts file or
  11 * board init file have been added. Detailed datasheet on which this
  12 * development is based is available here:
  13 *
  14 *  http://natisbad.org/NAS/refs/GMT_EDS-762_763-080710-0.2.pdf
  15 *
  16 * Headers from previous developments have been kept below:
  17 *
  18 * Copyright (c) 2009 LaCie
  19 *
  20 * Author: Olivier Mouchet <olivier.mouchet@gmail.com>
  21 *
  22 * based on g760a code written by Herbert Valerio Riedel <hvr@gnu.org>
  23 * Copyright (C) 2007  Herbert Valerio Riedel <hvr@gnu.org>
  24 *
  25 * g762: minimal datasheet available at:
  26 *       http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf
  27 *
  28 * This program is free software; you can redistribute it and/or modify
  29 * it under the terms of the GNU General Public License as published by
  30 * the Free Software Foundation; either version 2 of the License, or
  31 * (at your option) any later version.
  32 *
  33 * This program is distributed in the hope that it will be useful,
  34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36 * GNU General Public License for more details.
  37 *
  38 * You should have received a copy of the GNU General Public License
  39 * along with this program; if not, write to the Free Software
  40 * Foundation.
  41 */
  42
  43#include <linux/device.h>
  44#include <linux/module.h>
  45#include <linux/init.h>
  46#include <linux/jiffies.h>
  47#include <linux/i2c.h>
  48#include <linux/hwmon.h>
  49#include <linux/hwmon-sysfs.h>
  50#include <linux/err.h>
  51#include <linux/mutex.h>
  52#include <linux/kernel.h>
  53#include <linux/clk.h>
  54#include <linux/of.h>
  55#include <linux/of_device.h>
  56#include <linux/platform_data/g762.h>
  57
  58#define DRVNAME "g762"
  59
  60static const struct i2c_device_id g762_id[] = {
  61	{ "g762", 0 },
  62	{ "g763", 0 },
  63	{ }
  64};
  65MODULE_DEVICE_TABLE(i2c, g762_id);
  66
  67enum g762_regs {
  68	G762_REG_SET_CNT  = 0x00,
  69	G762_REG_ACT_CNT  = 0x01,
  70	G762_REG_FAN_STA  = 0x02,
  71	G762_REG_SET_OUT  = 0x03,
  72	G762_REG_FAN_CMD1 = 0x04,
  73	G762_REG_FAN_CMD2 = 0x05,
  74};
  75
  76/* Config register bits */
  77#define G762_REG_FAN_CMD1_DET_FAN_FAIL  0x80 /* enable fan_fail signal */
  78#define G762_REG_FAN_CMD1_DET_FAN_OOC   0x40 /* enable fan_out_of_control */
  79#define G762_REG_FAN_CMD1_OUT_MODE      0x20 /* out mode: PWM or DC */
  80#define G762_REG_FAN_CMD1_FAN_MODE      0x10 /* fan mode: closed/open-loop */
  81#define G762_REG_FAN_CMD1_CLK_DIV_ID1   0x08 /* clock divisor value */
  82#define G762_REG_FAN_CMD1_CLK_DIV_ID0   0x04
  83#define G762_REG_FAN_CMD1_PWM_POLARITY  0x02 /* PWM polarity */
  84#define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */
  85
  86#define G762_REG_FAN_CMD2_GEAR_MODE_1   0x08 /* fan gear mode */
  87#define G762_REG_FAN_CMD2_GEAR_MODE_0   0x04
  88#define G762_REG_FAN_CMD2_FAN_STARTV_1  0x02 /* fan startup voltage */
  89#define G762_REG_FAN_CMD2_FAN_STARTV_0  0x01
  90
  91#define G762_REG_FAN_STA_FAIL           0x02 /* fan fail */
  92#define G762_REG_FAN_STA_OOC            0x01 /* fan out of control */
  93
  94/* Config register values */
  95#define G762_OUT_MODE_PWM            1
  96#define G762_OUT_MODE_DC             0
  97
  98#define G762_FAN_MODE_CLOSED_LOOP    2
  99#define G762_FAN_MODE_OPEN_LOOP      1
 100
 101#define G762_PWM_POLARITY_NEGATIVE   1
 102#define G762_PWM_POLARITY_POSITIVE   0
 103
 104/* Register data is read (and cached) at most once per second. */
 105#define G762_UPDATE_INTERVAL    HZ
 106
 107/*
 108 * Extract pulse count per fan revolution value (2 or 4) from given
 109 * FAN_CMD1 register value.
 110 */
 111#define G762_PULSE_FROM_REG(reg) \
 112	((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
 113
 114/*
 115 * Extract fan clock divisor (1, 2, 4 or 8) from given FAN_CMD1
 116 * register value.
 117 */
 118#define G762_CLKDIV_FROM_REG(reg) \
 119	(1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 |	\
 120			 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
 121
 122/*
 123 * Extract fan gear mode multiplier value (0, 2 or 4) from given
 124 * FAN_CMD2 register value.
 125 */
 126#define G762_GEARMULT_FROM_REG(reg) \
 127	(1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 |	\
 128			 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
 129
 130struct g762_data {
 131	struct i2c_client *client;
 132	struct clk *clk;
 133
 134	/* update mutex */
 135	struct mutex update_lock;
 136
 137	/* board specific parameters. */
 138	u32 clk_freq;
 139
 140	/* g762 register cache */
 141	bool valid;
 142	unsigned long last_updated; /* in jiffies */
 143
 144	u8 set_cnt;  /* controls fan rotation speed in closed-loop mode */
 145	u8 act_cnt;  /* provides access to current fan RPM value */
 146	u8 fan_sta;  /* bit 0: set when actual fan speed is more than
 147		      *        25% outside requested fan speed
 148		      * bit 1: set when no transition occurs on fan
 149		      *        pin for 0.7s
 150		      */
 151	u8 set_out;  /* controls fan rotation speed in open-loop mode */
 152	u8 fan_cmd1; /*   0: FG_PLS_ID0 FG pulses count per revolution
 153		      *      0: 2 counts per revolution
 154		      *      1: 4 counts per revolution
 155		      *   1: PWM_POLARITY 1: negative_duty
 156		      *                   0: positive_duty
 157		      * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1]
 158		      *         00: Divide fan clock by 1
 159		      *         01: Divide fan clock by 2
 160		      *         10: Divide fan clock by 4
 161		      *         11: Divide fan clock by 8
 162		      *   4: FAN_MODE 1:closed-loop, 0:open-loop
 163		      *   5: OUT_MODE 1:PWM, 0:DC
 164		      *   6: DET_FAN_OOC enable "fan ooc" status
 165		      *   7: DET_FAN_FAIL enable "fan fail" status
 166		      */
 167	u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code
 168		      * 2,3: FG_GEAR_MODE
 169		      *         00: multiplier = 1
 170		      *         01: multiplier = 2
 171		      *         10: multiplier = 4
 172		      *   4: Mask ALERT# (g763 only)
 173		      */
 174};
 175
 176/*
 177 * Convert count value from fan controller register (FAN_SET_CNT) into fan
 178 * speed RPM value. Note that the datasheet documents a basic formula;
 179 * influence of additional parameters (fan clock divisor, fan gear mode)
 180 * have been infered from examples in the datasheet and tests.
 181 */
 182static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
 183					u8 clk_div, u8 gear_mult)
 184{
 185	if (cnt == 0xff)  /* setting cnt to 255 stops the fan */
 186		return 0;
 187
 188	return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
 189}
 190
 191/*
 192 * Convert fan RPM value from sysfs into count value for fan controller
 193 * register (FAN_SET_CNT).
 194 */
 195static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
 196					 u8 clk_div, u8 gear_mult)
 197{
 198	unsigned long f1 = clk_freq * 30 * gear_mult;
 199	unsigned long f2 = p * clk_div;
 200
 201	if (!rpm)	/* to stop the fan, set cnt to 255 */
 202		return 0xff;
 203
 204	rpm = clamp_val(rpm, f1 / (255 * f2), ULONG_MAX / f2);
 205	return DIV_ROUND_CLOSEST(f1, rpm * f2);
 206}
 207
 208/* helper to grab and cache data, at most one time per second */
 209static struct g762_data *g762_update_client(struct device *dev)
 210{
 211	struct g762_data *data = dev_get_drvdata(dev);
 212	struct i2c_client *client = data->client;
 213	int ret = 0;
 214
 215	mutex_lock(&data->update_lock);
 216	if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
 217	    likely(data->valid))
 218		goto out;
 219
 220	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
 221	if (ret < 0)
 222		goto out;
 223	data->set_cnt = ret;
 224
 225	ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
 226	if (ret < 0)
 227		goto out;
 228	data->act_cnt = ret;
 229
 230	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
 231	if (ret < 0)
 232		goto out;
 233	data->fan_sta = ret;
 234
 235	ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
 236	if (ret < 0)
 237		goto out;
 238	data->set_out = ret;
 239
 240	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
 241	if (ret < 0)
 242		goto out;
 243	data->fan_cmd1 = ret;
 244
 245	ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
 246	if (ret < 0)
 247		goto out;
 248	data->fan_cmd2 = ret;
 249
 250	data->last_updated = jiffies;
 251	data->valid = true;
 252 out:
 253	mutex_unlock(&data->update_lock);
 254
 255	if (ret < 0) /* upon error, encode it in return value */
 256		data = ERR_PTR(ret);
 257
 258	return data;
 259}
 260
 261/* helpers for writing hardware parameters */
 262
 263/*
 264 * Set input clock frequency received on CLK pin of the chip. Accepted values
 265 * are between 0 and 0xffffff. If zero is given, then default frequency
 266 * (32,768Hz) is used. Note that clock frequency is a characteristic of the
 267 * system but an internal parameter, i.e. value is not passed to the device.
 268 */
 269static int do_set_clk_freq(struct device *dev, unsigned long val)
 270{
 271	struct g762_data *data = dev_get_drvdata(dev);
 272
 273	if (val > 0xffffff)
 274		return -EINVAL;
 275	if (!val)
 276		val = 32768;
 277
 278	data->clk_freq = val;
 279
 280	return 0;
 281}
 282
 283/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */
 284static int do_set_pwm_mode(struct device *dev, unsigned long val)
 285{
 286	struct g762_data *data = g762_update_client(dev);
 287	int ret;
 288
 289	if (IS_ERR(data))
 290		return PTR_ERR(data);
 291
 292	mutex_lock(&data->update_lock);
 293	switch (val) {
 294	case G762_OUT_MODE_PWM:
 295		data->fan_cmd1 |=  G762_REG_FAN_CMD1_OUT_MODE;
 296		break;
 297	case G762_OUT_MODE_DC:
 298		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
 299		break;
 300	default:
 301		ret = -EINVAL;
 302		goto out;
 303	}
 304	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 305					data->fan_cmd1);
 306	data->valid = false;
 307 out:
 308	mutex_unlock(&data->update_lock);
 309
 310	return ret;
 311}
 312
 313/* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */
 314static int do_set_fan_div(struct device *dev, unsigned long val)
 315{
 316	struct g762_data *data = g762_update_client(dev);
 317	int ret;
 318
 319	if (IS_ERR(data))
 320		return PTR_ERR(data);
 321
 322	mutex_lock(&data->update_lock);
 323	switch (val) {
 324	case 1:
 325		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
 326		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
 327		break;
 328	case 2:
 329		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
 330		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
 331		break;
 332	case 4:
 333		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
 334		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
 335		break;
 336	case 8:
 337		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID0;
 338		data->fan_cmd1 |=  G762_REG_FAN_CMD1_CLK_DIV_ID1;
 339		break;
 340	default:
 341		ret = -EINVAL;
 342		goto out;
 343	}
 344	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 345					data->fan_cmd1);
 346	data->valid = false;
 347 out:
 348	mutex_unlock(&data->update_lock);
 349
 350	return ret;
 351}
 352
 353/* Set fan gear mode. Accepts either 0, 1 or 2. */
 354static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
 355{
 356	struct g762_data *data = g762_update_client(dev);
 357	int ret;
 358
 359	if (IS_ERR(data))
 360		return PTR_ERR(data);
 361
 362	mutex_lock(&data->update_lock);
 363	switch (val) {
 364	case 0:
 365		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
 366		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
 367		break;
 368	case 1:
 369		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_0;
 370		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
 371		break;
 372	case 2:
 373		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
 374		data->fan_cmd2 |=  G762_REG_FAN_CMD2_GEAR_MODE_1;
 375		break;
 376	default:
 377		ret = -EINVAL;
 378		goto out;
 379	}
 380	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
 381					data->fan_cmd2);
 382	data->valid = false;
 383 out:
 384	mutex_unlock(&data->update_lock);
 385
 386	return ret;
 387}
 388
 389/* Set number of fan pulses per revolution. Accepts either 2 or 4. */
 390static int do_set_fan_pulses(struct device *dev, unsigned long val)
 391{
 392	struct g762_data *data = g762_update_client(dev);
 393	int ret;
 394
 395	if (IS_ERR(data))
 396		return PTR_ERR(data);
 397
 398	mutex_lock(&data->update_lock);
 399	switch (val) {
 400	case 2:
 401		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
 402		break;
 403	case 4:
 404		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PULSE_PER_REV;
 405		break;
 406	default:
 407		ret = -EINVAL;
 408		goto out;
 409	}
 410	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 411					data->fan_cmd1);
 412	data->valid = false;
 413 out:
 414	mutex_unlock(&data->update_lock);
 415
 416	return ret;
 417}
 418
 419/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */
 420static int do_set_pwm_enable(struct device *dev, unsigned long val)
 421{
 422	struct g762_data *data = g762_update_client(dev);
 423	int ret;
 424
 425	if (IS_ERR(data))
 426		return PTR_ERR(data);
 427
 428	mutex_lock(&data->update_lock);
 429	switch (val) {
 430	case G762_FAN_MODE_CLOSED_LOOP:
 431		data->fan_cmd1 |=  G762_REG_FAN_CMD1_FAN_MODE;
 432		break;
 433	case G762_FAN_MODE_OPEN_LOOP:
 434		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
 435		/*
 436		 * BUG FIX: if SET_CNT register value is 255 then, for some
 437		 * unknown reason, fan will not rotate as expected, no matter
 438		 * the value of SET_OUT (to be specific, this seems to happen
 439		 * only in PWM mode). To workaround this bug, we give SET_CNT
 440		 * value of 254 if it is 255 when switching to open-loop.
 441		 */
 442		if (data->set_cnt == 0xff)
 443			i2c_smbus_write_byte_data(data->client,
 444						  G762_REG_SET_CNT, 254);
 445		break;
 446	default:
 447		ret = -EINVAL;
 448		goto out;
 449	}
 450
 451	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 452					data->fan_cmd1);
 453	data->valid = false;
 454 out:
 455	mutex_unlock(&data->update_lock);
 456
 457	return ret;
 458}
 459
 460/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */
 461static int do_set_pwm_polarity(struct device *dev, unsigned long val)
 462{
 463	struct g762_data *data = g762_update_client(dev);
 464	int ret;
 465
 466	if (IS_ERR(data))
 467		return PTR_ERR(data);
 468
 469	mutex_lock(&data->update_lock);
 470	switch (val) {
 471	case G762_PWM_POLARITY_POSITIVE:
 472		data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
 473		break;
 474	case G762_PWM_POLARITY_NEGATIVE:
 475		data->fan_cmd1 |=  G762_REG_FAN_CMD1_PWM_POLARITY;
 476		break;
 477	default:
 478		ret = -EINVAL;
 479		goto out;
 480	}
 481	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
 482					data->fan_cmd1);
 483	data->valid = false;
 484 out:
 485	mutex_unlock(&data->update_lock);
 486
 487	return ret;
 488}
 489
 490/*
 491 * Set pwm value. Accepts values between 0 (stops the fan) and
 492 * 255 (full speed). This only makes sense in open-loop mode.
 493 */
 494static int do_set_pwm(struct device *dev, unsigned long val)
 495{
 496	struct g762_data *data = dev_get_drvdata(dev);
 497	struct i2c_client *client = data->client;
 498	int ret;
 499
 500	if (val > 255)
 501		return -EINVAL;
 502
 503	mutex_lock(&data->update_lock);
 504	ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
 505	data->valid = false;
 506	mutex_unlock(&data->update_lock);
 507
 508	return ret;
 509}
 510
 511/*
 512 * Set fan RPM value. Can be called both in closed and open-loop mode
 513 * but effect will only be seen after closed-loop mode is configured.
 514 */
 515static int do_set_fan_target(struct device *dev, unsigned long val)
 516{
 517	struct g762_data *data = g762_update_client(dev);
 518	int ret;
 519
 520	if (IS_ERR(data))
 521		return PTR_ERR(data);
 522
 523	mutex_lock(&data->update_lock);
 524	data->set_cnt = cnt_from_rpm(val, data->clk_freq,
 525				     G762_PULSE_FROM_REG(data->fan_cmd1),
 526				     G762_CLKDIV_FROM_REG(data->fan_cmd1),
 527				     G762_GEARMULT_FROM_REG(data->fan_cmd2));
 528	ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
 529					data->set_cnt);
 530	data->valid = false;
 531	mutex_unlock(&data->update_lock);
 532
 533	return ret;
 534}
 535
 536/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */
 537static int do_set_fan_startv(struct device *dev, unsigned long val)
 538{
 539	struct g762_data *data = g762_update_client(dev);
 540	int ret;
 541
 542	if (IS_ERR(data))
 543		return PTR_ERR(data);
 544
 545	mutex_lock(&data->update_lock);
 546	switch (val) {
 547	case 0:
 548		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
 549		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
 550		break;
 551	case 1:
 552		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
 553		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
 554		break;
 555	case 2:
 556		data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
 557		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
 558		break;
 559	case 3:
 560		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_0;
 561		data->fan_cmd2 |=  G762_REG_FAN_CMD2_FAN_STARTV_1;
 562		break;
 563	default:
 564		ret = -EINVAL;
 565		goto out;
 566	}
 567	ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
 568					data->fan_cmd2);
 569	data->valid = false;
 570 out:
 571	mutex_unlock(&data->update_lock);
 572
 573	return ret;
 574}
 575
 576/*
 577 * Helper to import hardware characteristics from .dts file and push
 578 * those to the chip.
 579 */
 580
 581#ifdef CONFIG_OF
 582static const struct of_device_id g762_dt_match[] = {
 583	{ .compatible = "gmt,g762" },
 584	{ .compatible = "gmt,g763" },
 585	{ },
 586};
 587MODULE_DEVICE_TABLE(of, g762_dt_match);
 588
 589/*
 590 * Grab clock (a required property), enable it, get (fixed) clock frequency
 591 * and store it. Note: upon success, clock has been prepared and enabled; it
 592 * must later be unprepared and disabled (e.g. during module unloading) by a
 593 * call to g762_of_clock_disable(). Note that a reference to clock is kept
 594 * in our private data structure to be used in this function.
 595 */
 596static void g762_of_clock_disable(void *data)
 597{
 598	struct g762_data *g762 = data;
 599
 600	clk_disable_unprepare(g762->clk);
 601	clk_put(g762->clk);
 602}
 603
 604static int g762_of_clock_enable(struct i2c_client *client)
 605{
 606	struct g762_data *data;
 607	unsigned long clk_freq;
 608	struct clk *clk;
 609	int ret;
 610
 611	if (!client->dev.of_node)
 612		return 0;
 613
 614	clk = of_clk_get(client->dev.of_node, 0);
 615	if (IS_ERR(clk)) {
 616		dev_err(&client->dev, "failed to get clock\n");
 617		return PTR_ERR(clk);
 618	}
 619
 620	ret = clk_prepare_enable(clk);
 621	if (ret) {
 622		dev_err(&client->dev, "failed to enable clock\n");
 623		goto clk_put;
 624	}
 625
 626	clk_freq = clk_get_rate(clk);
 627	ret = do_set_clk_freq(&client->dev, clk_freq);
 628	if (ret) {
 629		dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
 630		goto clk_unprep;
 631	}
 632
 633	data = i2c_get_clientdata(client);
 634	data->clk = clk;
 635
 636	devm_add_action(&client->dev, g762_of_clock_disable, data);
 
 
 
 
 
 637	return 0;
 638
 639 clk_unprep:
 640	clk_disable_unprepare(clk);
 641
 642 clk_put:
 643	clk_put(clk);
 644
 645	return ret;
 646}
 647
 648static int g762_of_prop_import_one(struct i2c_client *client,
 649				   const char *pname,
 650				   int (*psetter)(struct device *dev,
 651						  unsigned long val))
 652{
 653	int ret;
 654	u32 pval;
 655
 656	if (of_property_read_u32(client->dev.of_node, pname, &pval))
 657		return 0;
 658
 659	dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
 660	ret = (*psetter)(&client->dev, pval);
 661	if (ret)
 662		dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
 663
 664	return ret;
 665}
 666
 667static int g762_of_prop_import(struct i2c_client *client)
 668{
 669	int ret;
 670
 671	if (!client->dev.of_node)
 672		return 0;
 673
 674	ret = g762_of_prop_import_one(client, "fan_gear_mode",
 675				      do_set_fan_gear_mode);
 676	if (ret)
 677		return ret;
 678
 679	ret = g762_of_prop_import_one(client, "pwm_polarity",
 680				      do_set_pwm_polarity);
 681	if (ret)
 682		return ret;
 683
 684	return g762_of_prop_import_one(client, "fan_startv",
 685				       do_set_fan_startv);
 686}
 687
 688#else
 689static int g762_of_prop_import(struct i2c_client *client)
 690{
 691	return 0;
 692}
 693
 694static int g762_of_clock_enable(struct i2c_client *client)
 695{
 696	return 0;
 697}
 698#endif
 699
 700/*
 701 * Helper to import hardware characteristics from .dts file and push
 702 * those to the chip.
 703 */
 704
 705static int g762_pdata_prop_import(struct i2c_client *client)
 706{
 707	struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
 708	int ret;
 709
 710	if (!pdata)
 711		return 0;
 712
 713	ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
 714	if (ret)
 715		return ret;
 716
 717	ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
 718	if (ret)
 719		return ret;
 720
 721	ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
 722	if (ret)
 723		return ret;
 724
 725	return do_set_clk_freq(&client->dev, pdata->clk_freq);
 726}
 727
 728/*
 729 * sysfs attributes
 730 */
 731
 732/*
 733 * Read function for fan1_input sysfs file. Return current fan RPM value, or
 734 * 0 if fan is out of control.
 735 */
 736static ssize_t fan1_input_show(struct device *dev,
 737			       struct device_attribute *da, char *buf)
 738{
 739	struct g762_data *data = g762_update_client(dev);
 740	unsigned int rpm = 0;
 741
 742	if (IS_ERR(data))
 743		return PTR_ERR(data);
 744
 745	mutex_lock(&data->update_lock);
 746	/* reverse logic: fan out of control reporting is enabled low */
 747	if (data->fan_sta & G762_REG_FAN_STA_OOC) {
 748		rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
 749				   G762_PULSE_FROM_REG(data->fan_cmd1),
 750				   G762_CLKDIV_FROM_REG(data->fan_cmd1),
 751				   G762_GEARMULT_FROM_REG(data->fan_cmd2));
 752	}
 753	mutex_unlock(&data->update_lock);
 754
 755	return sprintf(buf, "%u\n", rpm);
 756}
 757
 758/*
 759 * Read and write functions for pwm1_mode sysfs file. Get and set fan speed
 760 * control mode i.e. PWM (1) or DC (0).
 761 */
 762static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da,
 763			      char *buf)
 764{
 765	struct g762_data *data = g762_update_client(dev);
 766
 767	if (IS_ERR(data))
 768		return PTR_ERR(data);
 769
 770	return sprintf(buf, "%d\n",
 771		       !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
 772}
 773
 774static ssize_t pwm1_mode_store(struct device *dev,
 775			       struct device_attribute *da, const char *buf,
 776			       size_t count)
 777{
 778	unsigned long val;
 779	int ret;
 780
 781	if (kstrtoul(buf, 10, &val))
 782		return -EINVAL;
 783
 784	ret = do_set_pwm_mode(dev, val);
 785	if (ret < 0)
 786		return ret;
 787
 788	return count;
 789}
 790
 791/*
 792 * Read and write functions for fan1_div sysfs file. Get and set fan
 793 * controller prescaler value
 794 */
 795static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da,
 796			     char *buf)
 797{
 798	struct g762_data *data = g762_update_client(dev);
 799
 800	if (IS_ERR(data))
 801		return PTR_ERR(data);
 802
 803	return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
 804}
 805
 806static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
 807			      const char *buf, size_t count)
 808{
 809	unsigned long val;
 810	int ret;
 811
 812	if (kstrtoul(buf, 10, &val))
 813		return -EINVAL;
 814
 815	ret = do_set_fan_div(dev, val);
 816	if (ret < 0)
 817		return ret;
 818
 819	return count;
 820}
 821
 822/*
 823 * Read and write functions for fan1_pulses sysfs file. Get and set number
 824 * of tachometer pulses per fan revolution.
 825 */
 826static ssize_t fan1_pulses_show(struct device *dev,
 827				struct device_attribute *da, char *buf)
 828{
 829	struct g762_data *data = g762_update_client(dev);
 830
 831	if (IS_ERR(data))
 832		return PTR_ERR(data);
 833
 834	return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
 835}
 836
 837static ssize_t fan1_pulses_store(struct device *dev,
 838				 struct device_attribute *da, const char *buf,
 839				 size_t count)
 840{
 841	unsigned long val;
 842	int ret;
 843
 844	if (kstrtoul(buf, 10, &val))
 845		return -EINVAL;
 846
 847	ret = do_set_fan_pulses(dev, val);
 848	if (ret < 0)
 849		return ret;
 850
 851	return count;
 852}
 853
 854/*
 855 * Read and write functions for pwm1_enable. Get and set fan speed control mode
 856 * (i.e. closed or open-loop).
 857 *
 858 * Following documentation about hwmon's sysfs interface, a pwm1_enable node
 859 * should accept the following:
 860 *
 861 *  0 : no fan speed control (i.e. fan at full speed)
 862 *  1 : manual fan speed control enabled (use pwm[1-*]) (open-loop)
 863 *  2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop)
 864 *
 865 * but we do not accept 0 as this mode is not natively supported by the chip
 866 * and it is not emulated by g762 driver. -EINVAL is returned in this case.
 867 */
 868static ssize_t pwm1_enable_show(struct device *dev,
 869				struct device_attribute *da, char *buf)
 870{
 871	struct g762_data *data = g762_update_client(dev);
 872
 873	if (IS_ERR(data))
 874		return PTR_ERR(data);
 875
 876	return sprintf(buf, "%d\n",
 877		       (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
 878}
 879
 880static ssize_t pwm1_enable_store(struct device *dev,
 881				 struct device_attribute *da, const char *buf,
 882				 size_t count)
 883{
 884	unsigned long val;
 885	int ret;
 886
 887	if (kstrtoul(buf, 10, &val))
 888		return -EINVAL;
 889
 890	ret = do_set_pwm_enable(dev, val);
 891	if (ret < 0)
 892		return ret;
 893
 894	return count;
 895}
 896
 897/*
 898 * Read and write functions for pwm1 sysfs file. Get and set pwm value
 899 * (which affects fan speed) in open-loop mode. 0 stops the fan and 255
 900 * makes it run at full speed.
 901 */
 902static ssize_t pwm1_show(struct device *dev, struct device_attribute *da,
 903			 char *buf)
 904{
 905	struct g762_data *data = g762_update_client(dev);
 906
 907	if (IS_ERR(data))
 908		return PTR_ERR(data);
 909
 910	return sprintf(buf, "%d\n", data->set_out);
 911}
 912
 913static ssize_t pwm1_store(struct device *dev, struct device_attribute *da,
 914			  const char *buf, size_t count)
 915{
 916	unsigned long val;
 917	int ret;
 918
 919	if (kstrtoul(buf, 10, &val))
 920		return -EINVAL;
 921
 922	ret = do_set_pwm(dev, val);
 923	if (ret < 0)
 924		return ret;
 925
 926	return count;
 927}
 928
 929/*
 930 * Read and write function for fan1_target sysfs file. Get/set the fan speed in
 931 * closed-loop mode. Speed is given as a RPM value; then the chip will regulate
 932 * the fan speed using pulses from fan tachometer.
 933 *
 934 * Refer to rpm_from_cnt() implementation above to get info about count number
 935 * calculation.
 936 *
 937 * Also note that due to rounding errors it is possible that you don't read
 938 * back exactly the value you have set.
 939 */
 940static ssize_t fan1_target_show(struct device *dev,
 941				struct device_attribute *da, char *buf)
 942{
 943	struct g762_data *data = g762_update_client(dev);
 944	unsigned int rpm;
 945
 946	if (IS_ERR(data))
 947		return PTR_ERR(data);
 948
 949	mutex_lock(&data->update_lock);
 950	rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
 951			   G762_PULSE_FROM_REG(data->fan_cmd1),
 952			   G762_CLKDIV_FROM_REG(data->fan_cmd1),
 953			   G762_GEARMULT_FROM_REG(data->fan_cmd2));
 954	mutex_unlock(&data->update_lock);
 955
 956	return sprintf(buf, "%u\n", rpm);
 957}
 958
 959static ssize_t fan1_target_store(struct device *dev,
 960				 struct device_attribute *da, const char *buf,
 961				 size_t count)
 962{
 963	unsigned long val;
 964	int ret;
 965
 966	if (kstrtoul(buf, 10, &val))
 967		return -EINVAL;
 968
 969	ret = do_set_fan_target(dev, val);
 970	if (ret < 0)
 971		return ret;
 972
 973	return count;
 974}
 975
 976/* read function for fan1_fault sysfs file. */
 977static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da,
 978			       char *buf)
 979{
 980	struct g762_data *data = g762_update_client(dev);
 981
 982	if (IS_ERR(data))
 983		return PTR_ERR(data);
 984
 985	return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
 986}
 987
 988/*
 989 * read function for fan1_alarm sysfs file. Note that OOC condition is
 990 * enabled low
 991 */
 992static ssize_t fan1_alarm_show(struct device *dev,
 993			       struct device_attribute *da, char *buf)
 994{
 995	struct g762_data *data = g762_update_client(dev);
 996
 997	if (IS_ERR(data))
 998		return PTR_ERR(data);
 999
1000	return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
1001}
1002
1003static DEVICE_ATTR_RW(pwm1);
1004static DEVICE_ATTR_RW(pwm1_mode);
1005static DEVICE_ATTR_RW(pwm1_enable);
1006static DEVICE_ATTR_RO(fan1_input);
1007static DEVICE_ATTR_RO(fan1_alarm);
1008static DEVICE_ATTR_RO(fan1_fault);
1009static DEVICE_ATTR_RW(fan1_target);
1010static DEVICE_ATTR_RW(fan1_div);
1011static DEVICE_ATTR_RW(fan1_pulses);
1012
1013/* Driver data */
1014static struct attribute *g762_attrs[] = {
1015	&dev_attr_fan1_input.attr,
1016	&dev_attr_fan1_alarm.attr,
1017	&dev_attr_fan1_fault.attr,
1018	&dev_attr_fan1_target.attr,
1019	&dev_attr_fan1_div.attr,
1020	&dev_attr_fan1_pulses.attr,
1021	&dev_attr_pwm1.attr,
1022	&dev_attr_pwm1_mode.attr,
1023	&dev_attr_pwm1_enable.attr,
1024	NULL
1025};
1026
1027ATTRIBUTE_GROUPS(g762);
1028
1029/*
1030 * Enable both fan failure detection and fan out of control protection. The
1031 * function does not protect change/access to data structure; it must thus
1032 * only be called during initialization.
1033 */
1034static inline int g762_fan_init(struct device *dev)
1035{
1036	struct g762_data *data = g762_update_client(dev);
1037
1038	if (IS_ERR(data))
1039		return PTR_ERR(data);
1040
1041	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1042	data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1043	data->valid = false;
1044
1045	return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
1046					 data->fan_cmd1);
1047}
1048
1049static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
1050{
1051	struct device *dev = &client->dev;
1052	struct device *hwmon_dev;
1053	struct g762_data *data;
1054	int ret;
1055
1056	if (!i2c_check_functionality(client->adapter,
1057				     I2C_FUNC_SMBUS_BYTE_DATA))
1058		return -ENODEV;
1059
1060	data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
1061	if (!data)
1062		return -ENOMEM;
1063
1064	i2c_set_clientdata(client, data);
1065	data->client = client;
1066	mutex_init(&data->update_lock);
1067
1068	/* Enable fan failure detection and fan out of control protection */
1069	ret = g762_fan_init(dev);
1070	if (ret)
1071		return ret;
1072
1073	/* Get configuration via DT ... */
1074	ret = g762_of_clock_enable(client);
1075	if (ret)
1076		return ret;
1077	ret = g762_of_prop_import(client);
1078	if (ret)
1079		return ret;
1080	/* ... or platform_data */
1081	ret = g762_pdata_prop_import(client);
1082	if (ret)
1083		return ret;
1084
1085	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1086							    data, g762_groups);
1087	return PTR_ERR_OR_ZERO(hwmon_dev);
1088}
1089
1090static struct i2c_driver g762_driver = {
1091	.driver = {
1092		.name = DRVNAME,
1093		.of_match_table = of_match_ptr(g762_dt_match),
1094	},
1095	.probe	  = g762_probe,
1096	.id_table = g762_id,
1097};
1098
1099module_i2c_driver(g762_driver);
1100
1101MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1102MODULE_DESCRIPTION("GMT G762/G763 driver");
1103MODULE_LICENSE("GPL");