g762.c (b9f12a5d97f652c77ef6803dccd0d40d1290f5be) | g762.c (398e16db62620dd80a237c5043bec5c9335ac92c) |
---|---|
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 --- 114 unchanged lines hidden (view full) --- 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 { | 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 --- 114 unchanged lines hidden (view full) --- 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 device *hwmon_dev; | 131 struct device *hwmon_dev; |
132 struct i2c_client *client; |
|
133 struct clk *clk; 134 135 /* update mutex */ 136 struct mutex update_lock; 137 138 /* board specific parameters. */ 139 u32 clk_freq; 140 --- 60 unchanged lines hidden (view full) --- 201 202 return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)), 203 0, 255); 204} 205 206/* helper to grab and cache data, at most one time per second */ 207static struct g762_data *g762_update_client(struct device *dev) 208{ | 133 struct clk *clk; 134 135 /* update mutex */ 136 struct mutex update_lock; 137 138 /* board specific parameters. */ 139 u32 clk_freq; 140 --- 60 unchanged lines hidden (view full) --- 201 202 return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)), 203 0, 255); 204} 205 206/* helper to grab and cache data, at most one time per second */ 207static struct g762_data *g762_update_client(struct device *dev) 208{ |
209 struct i2c_client *client = to_i2c_client(dev); 210 struct g762_data *data = i2c_get_clientdata(client); | 209 struct g762_data *data = dev_get_drvdata(dev); 210 struct i2c_client *client = data->client; |
211 int ret = 0; 212 213 mutex_lock(&data->update_lock); 214 if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) && 215 likely(data->valid)) 216 goto out; 217 218 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT); --- 42 unchanged lines hidden (view full) --- 261/* 262 * Set input clock frequency received on CLK pin of the chip. Accepted values 263 * are between 0 and 0xffffff. If zero is given, then default frequency 264 * (32,768Hz) is used. Note that clock frequency is a characteristic of the 265 * system but an internal parameter, i.e. value is not passed to the device. 266 */ 267static int do_set_clk_freq(struct device *dev, unsigned long val) 268{ | 211 int ret = 0; 212 213 mutex_lock(&data->update_lock); 214 if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) && 215 likely(data->valid)) 216 goto out; 217 218 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT); --- 42 unchanged lines hidden (view full) --- 261/* 262 * Set input clock frequency received on CLK pin of the chip. Accepted values 263 * are between 0 and 0xffffff. If zero is given, then default frequency 264 * (32,768Hz) is used. Note that clock frequency is a characteristic of the 265 * system but an internal parameter, i.e. value is not passed to the device. 266 */ 267static int do_set_clk_freq(struct device *dev, unsigned long val) 268{ |
269 struct i2c_client *client = to_i2c_client(dev); 270 struct g762_data *data = i2c_get_clientdata(client); | 269 struct g762_data *data = dev_get_drvdata(dev); |
271 272 if (val > 0xffffff) 273 return -EINVAL; 274 if (!val) 275 val = 32768; 276 277 data->clk_freq = val; 278 279 return 0; 280} 281 282/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ 283static int do_set_pwm_mode(struct device *dev, unsigned long val) 284{ | 270 271 if (val > 0xffffff) 272 return -EINVAL; 273 if (!val) 274 val = 32768; 275 276 data->clk_freq = val; 277 278 return 0; 279} 280 281/* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ 282static int do_set_pwm_mode(struct device *dev, unsigned long val) 283{ |
285 struct i2c_client *client = to_i2c_client(dev); | |
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 } | 284 struct g762_data *data = g762_update_client(dev); 285 int ret; 286 287 if (IS_ERR(data)) 288 return PTR_ERR(data); 289 290 mutex_lock(&data->update_lock); 291 switch (val) { 292 case G762_OUT_MODE_PWM: 293 data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE; 294 break; 295 case G762_OUT_MODE_DC: 296 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE; 297 break; 298 default: 299 ret = -EINVAL; 300 goto out; 301 } |
304 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, | 302 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{ | 303 data->fan_cmd1); 304 data->valid = false; 305 out: 306 mutex_unlock(&data->update_lock); 307 308 return ret; 309} 310 311/* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */ 312static int do_set_fan_div(struct device *dev, unsigned long val) 313{ |
316 struct i2c_client *client = to_i2c_client(dev); | |
317 struct g762_data *data = g762_update_client(dev); 318 int ret; 319 320 if (IS_ERR(data)) 321 return PTR_ERR(data); 322 323 mutex_lock(&data->update_lock); 324 switch (val) { --- 12 unchanged lines hidden (view full) --- 337 case 8: 338 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0; 339 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1; 340 break; 341 default: 342 ret = -EINVAL; 343 goto out; 344 } | 314 struct g762_data *data = g762_update_client(dev); 315 int ret; 316 317 if (IS_ERR(data)) 318 return PTR_ERR(data); 319 320 mutex_lock(&data->update_lock); 321 switch (val) { --- 12 unchanged lines hidden (view full) --- 334 case 8: 335 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0; 336 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1; 337 break; 338 default: 339 ret = -EINVAL; 340 goto out; 341 } |
345 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, | 342 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, |
346 data->fan_cmd1); 347 data->valid = false; 348 out: 349 mutex_unlock(&data->update_lock); 350 351 return ret; 352} 353 354/* Set fan gear mode. Accepts either 0, 1 or 2. */ 355static int do_set_fan_gear_mode(struct device *dev, unsigned long val) 356{ | 343 data->fan_cmd1); 344 data->valid = false; 345 out: 346 mutex_unlock(&data->update_lock); 347 348 return ret; 349} 350 351/* Set fan gear mode. Accepts either 0, 1 or 2. */ 352static int do_set_fan_gear_mode(struct device *dev, unsigned long val) 353{ |
357 struct i2c_client *client = to_i2c_client(dev); | |
358 struct g762_data *data = g762_update_client(dev); 359 int ret; 360 361 if (IS_ERR(data)) 362 return PTR_ERR(data); 363 364 mutex_lock(&data->update_lock); 365 switch (val) { --- 8 unchanged lines hidden (view full) --- 374 case 2: 375 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0; 376 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1; 377 break; 378 default: 379 ret = -EINVAL; 380 goto out; 381 } | 354 struct g762_data *data = g762_update_client(dev); 355 int ret; 356 357 if (IS_ERR(data)) 358 return PTR_ERR(data); 359 360 mutex_lock(&data->update_lock); 361 switch (val) { --- 8 unchanged lines hidden (view full) --- 370 case 2: 371 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0; 372 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1; 373 break; 374 default: 375 ret = -EINVAL; 376 goto out; 377 } |
382 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2, | 378 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, |
383 data->fan_cmd2); 384 data->valid = false; 385 out: 386 mutex_unlock(&data->update_lock); 387 388 return ret; 389} 390 391/* Set number of fan pulses per revolution. Accepts either 2 or 4. */ 392static int do_set_fan_pulses(struct device *dev, unsigned long val) 393{ | 379 data->fan_cmd2); 380 data->valid = false; 381 out: 382 mutex_unlock(&data->update_lock); 383 384 return ret; 385} 386 387/* Set number of fan pulses per revolution. Accepts either 2 or 4. */ 388static int do_set_fan_pulses(struct device *dev, unsigned long val) 389{ |
394 struct i2c_client *client = to_i2c_client(dev); | |
395 struct g762_data *data = g762_update_client(dev); 396 int ret; 397 398 if (IS_ERR(data)) 399 return PTR_ERR(data); 400 401 mutex_lock(&data->update_lock); 402 switch (val) { 403 case 2: 404 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV; 405 break; 406 case 4: 407 data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV; 408 break; 409 default: 410 ret = -EINVAL; 411 goto out; 412 } | 390 struct g762_data *data = g762_update_client(dev); 391 int ret; 392 393 if (IS_ERR(data)) 394 return PTR_ERR(data); 395 396 mutex_lock(&data->update_lock); 397 switch (val) { 398 case 2: 399 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV; 400 break; 401 case 4: 402 data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV; 403 break; 404 default: 405 ret = -EINVAL; 406 goto out; 407 } |
413 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, | 408 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, |
414 data->fan_cmd1); 415 data->valid = false; 416 out: 417 mutex_unlock(&data->update_lock); 418 419 return ret; 420} 421 422/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ 423static int do_set_pwm_enable(struct device *dev, unsigned long val) 424{ | 409 data->fan_cmd1); 410 data->valid = false; 411 out: 412 mutex_unlock(&data->update_lock); 413 414 return ret; 415} 416 417/* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ 418static int do_set_pwm_enable(struct device *dev, unsigned long val) 419{ |
425 struct i2c_client *client = to_i2c_client(dev); | |
426 struct g762_data *data = g762_update_client(dev); 427 int ret; 428 429 if (IS_ERR(data)) 430 return PTR_ERR(data); 431 432 mutex_lock(&data->update_lock); 433 switch (val) { --- 5 unchanged lines hidden (view full) --- 439 /* 440 * BUG FIX: if SET_CNT register value is 255 then, for some 441 * unknown reason, fan will not rotate as expected, no matter 442 * the value of SET_OUT (to be specific, this seems to happen 443 * only in PWM mode). To workaround this bug, we give SET_CNT 444 * value of 254 if it is 255 when switching to open-loop. 445 */ 446 if (data->set_cnt == 0xff) | 420 struct g762_data *data = g762_update_client(dev); 421 int ret; 422 423 if (IS_ERR(data)) 424 return PTR_ERR(data); 425 426 mutex_lock(&data->update_lock); 427 switch (val) { --- 5 unchanged lines hidden (view full) --- 433 /* 434 * BUG FIX: if SET_CNT register value is 255 then, for some 435 * unknown reason, fan will not rotate as expected, no matter 436 * the value of SET_OUT (to be specific, this seems to happen 437 * only in PWM mode). To workaround this bug, we give SET_CNT 438 * value of 254 if it is 255 when switching to open-loop. 439 */ 440 if (data->set_cnt == 0xff) |
447 i2c_smbus_write_byte_data(client, G762_REG_SET_CNT, 448 254); | 441 i2c_smbus_write_byte_data(data->client, 442 G762_REG_SET_CNT, 254); |
449 break; 450 default: 451 ret = -EINVAL; 452 goto out; 453 } 454 | 443 break; 444 default: 445 ret = -EINVAL; 446 goto out; 447 } 448 |
455 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, | 449 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, |
456 data->fan_cmd1); 457 data->valid = false; 458 out: 459 mutex_unlock(&data->update_lock); 460 461 return ret; 462} 463 464/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */ 465static int do_set_pwm_polarity(struct device *dev, unsigned long val) 466{ | 450 data->fan_cmd1); 451 data->valid = false; 452 out: 453 mutex_unlock(&data->update_lock); 454 455 return ret; 456} 457 458/* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */ 459static int do_set_pwm_polarity(struct device *dev, unsigned long val) 460{ |
467 struct i2c_client *client = to_i2c_client(dev); | |
468 struct g762_data *data = g762_update_client(dev); 469 int ret; 470 471 if (IS_ERR(data)) 472 return PTR_ERR(data); 473 474 mutex_lock(&data->update_lock); 475 switch (val) { 476 case G762_PWM_POLARITY_POSITIVE: 477 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY; 478 break; 479 case G762_PWM_POLARITY_NEGATIVE: 480 data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY; 481 break; 482 default: 483 ret = -EINVAL; 484 goto out; 485 } | 461 struct g762_data *data = g762_update_client(dev); 462 int ret; 463 464 if (IS_ERR(data)) 465 return PTR_ERR(data); 466 467 mutex_lock(&data->update_lock); 468 switch (val) { 469 case G762_PWM_POLARITY_POSITIVE: 470 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY; 471 break; 472 case G762_PWM_POLARITY_NEGATIVE: 473 data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY; 474 break; 475 default: 476 ret = -EINVAL; 477 goto out; 478 } |
486 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, | 479 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, |
487 data->fan_cmd1); 488 data->valid = false; 489 out: 490 mutex_unlock(&data->update_lock); 491 492 return ret; 493} 494 495/* 496 * Set pwm value. Accepts values between 0 (stops the fan) and 497 * 255 (full speed). This only makes sense in open-loop mode. 498 */ 499static int do_set_pwm(struct device *dev, unsigned long val) 500{ | 480 data->fan_cmd1); 481 data->valid = false; 482 out: 483 mutex_unlock(&data->update_lock); 484 485 return ret; 486} 487 488/* 489 * Set pwm value. Accepts values between 0 (stops the fan) and 490 * 255 (full speed). This only makes sense in open-loop mode. 491 */ 492static int do_set_pwm(struct device *dev, unsigned long val) 493{ |
501 struct i2c_client *client = to_i2c_client(dev); 502 struct g762_data *data = i2c_get_clientdata(client); | 494 struct g762_data *data = dev_get_drvdata(dev); 495 struct i2c_client *client = data->client; |
503 int ret; 504 505 if (val > 255) 506 return -EINVAL; 507 508 mutex_lock(&data->update_lock); 509 ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val); 510 data->valid = false; 511 mutex_unlock(&data->update_lock); 512 513 return ret; 514} 515 516/* 517 * Set fan RPM value. Can be called both in closed and open-loop mode 518 * but effect will only be seen after closed-loop mode is configured. 519 */ 520static int do_set_fan_target(struct device *dev, unsigned long val) 521{ | 496 int ret; 497 498 if (val > 255) 499 return -EINVAL; 500 501 mutex_lock(&data->update_lock); 502 ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val); 503 data->valid = false; 504 mutex_unlock(&data->update_lock); 505 506 return ret; 507} 508 509/* 510 * Set fan RPM value. Can be called both in closed and open-loop mode 511 * but effect will only be seen after closed-loop mode is configured. 512 */ 513static int do_set_fan_target(struct device *dev, unsigned long val) 514{ |
522 struct i2c_client *client = to_i2c_client(dev); | |
523 struct g762_data *data = g762_update_client(dev); 524 int ret; 525 526 if (IS_ERR(data)) 527 return PTR_ERR(data); 528 529 mutex_lock(&data->update_lock); 530 data->set_cnt = cnt_from_rpm(val, data->clk_freq, 531 G762_PULSE_FROM_REG(data->fan_cmd1), 532 G762_CLKDIV_FROM_REG(data->fan_cmd1), 533 G762_GEARMULT_FROM_REG(data->fan_cmd2)); | 515 struct g762_data *data = g762_update_client(dev); 516 int ret; 517 518 if (IS_ERR(data)) 519 return PTR_ERR(data); 520 521 mutex_lock(&data->update_lock); 522 data->set_cnt = cnt_from_rpm(val, data->clk_freq, 523 G762_PULSE_FROM_REG(data->fan_cmd1), 524 G762_CLKDIV_FROM_REG(data->fan_cmd1), 525 G762_GEARMULT_FROM_REG(data->fan_cmd2)); |
534 ret = i2c_smbus_write_byte_data(client, G762_REG_SET_CNT, | 526 ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT, |
535 data->set_cnt); 536 data->valid = false; 537 mutex_unlock(&data->update_lock); 538 539 return ret; 540} 541 542/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */ 543static int do_set_fan_startv(struct device *dev, unsigned long val) 544{ | 527 data->set_cnt); 528 data->valid = false; 529 mutex_unlock(&data->update_lock); 530 531 return ret; 532} 533 534/* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */ 535static int do_set_fan_startv(struct device *dev, unsigned long val) 536{ |
545 struct i2c_client *client = to_i2c_client(dev); | |
546 struct g762_data *data = g762_update_client(dev); 547 int ret; 548 549 if (IS_ERR(data)) 550 return PTR_ERR(data); 551 552 mutex_lock(&data->update_lock); 553 switch (val) { --- 12 unchanged lines hidden (view full) --- 566 case 3: 567 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0; 568 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1; 569 break; 570 default: 571 ret = -EINVAL; 572 goto out; 573 } | 537 struct g762_data *data = g762_update_client(dev); 538 int ret; 539 540 if (IS_ERR(data)) 541 return PTR_ERR(data); 542 543 mutex_lock(&data->update_lock); 544 switch (val) { --- 12 unchanged lines hidden (view full) --- 557 case 3: 558 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0; 559 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1; 560 break; 561 default: 562 ret = -EINVAL; 563 goto out; 564 } |
574 ret = i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD2, | 565 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, |
575 data->fan_cmd2); 576 data->valid = false; 577 out: 578 mutex_unlock(&data->update_lock); 579 580 return ret; 581} 582 --- 438 unchanged lines hidden (view full) --- 1021static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL); 1022static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, 1023 get_fan_target, set_fan_target); 1024static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div); 1025static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, 1026 get_fan_pulses, set_fan_pulses); 1027 1028/* Driver data */ | 566 data->fan_cmd2); 567 data->valid = false; 568 out: 569 mutex_unlock(&data->update_lock); 570 571 return ret; 572} 573 --- 438 unchanged lines hidden (view full) --- 1012static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL); 1013static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, 1014 get_fan_target, set_fan_target); 1015static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div); 1016static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO, 1017 get_fan_pulses, set_fan_pulses); 1018 1019/* Driver data */ |
1029static struct attribute *g762_attributes[] = { | 1020static struct attribute *g762_attrs[] = { |
1030 &dev_attr_fan1_input.attr, 1031 &dev_attr_fan1_alarm.attr, 1032 &dev_attr_fan1_fault.attr, 1033 &dev_attr_fan1_target.attr, 1034 &dev_attr_fan1_div.attr, 1035 &dev_attr_fan1_pulses.attr, 1036 &dev_attr_pwm1.attr, 1037 &dev_attr_pwm1_mode.attr, 1038 &dev_attr_pwm1_enable.attr, 1039 NULL 1040}; 1041 | 1021 &dev_attr_fan1_input.attr, 1022 &dev_attr_fan1_alarm.attr, 1023 &dev_attr_fan1_fault.attr, 1024 &dev_attr_fan1_target.attr, 1025 &dev_attr_fan1_div.attr, 1026 &dev_attr_fan1_pulses.attr, 1027 &dev_attr_pwm1.attr, 1028 &dev_attr_pwm1_mode.attr, 1029 &dev_attr_pwm1_enable.attr, 1030 NULL 1031}; 1032 |
1042static const struct attribute_group g762_group = { 1043 .attrs = g762_attributes, 1044}; | 1033ATTRIBUTE_GROUPS(g762); |
1045 1046/* 1047 * Enable both fan failure detection and fan out of control protection. The 1048 * function does not protect change/access to data structure; it must thus 1049 * only be called during initialization. 1050 */ 1051static inline int g762_fan_init(struct device *dev) 1052{ | 1034 1035/* 1036 * Enable both fan failure detection and fan out of control protection. The 1037 * function does not protect change/access to data structure; it must thus 1038 * only be called during initialization. 1039 */ 1040static inline int g762_fan_init(struct device *dev) 1041{ |
1053 struct i2c_client *client = to_i2c_client(dev); | |
1054 struct g762_data *data = g762_update_client(dev); 1055 1056 if (IS_ERR(data)) 1057 return PTR_ERR(data); 1058 1059 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL; 1060 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC; 1061 data->valid = false; 1062 | 1042 struct g762_data *data = g762_update_client(dev); 1043 1044 if (IS_ERR(data)) 1045 return PTR_ERR(data); 1046 1047 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL; 1048 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC; 1049 data->valid = false; 1050 |
1063 return i2c_smbus_write_byte_data(client, G762_REG_FAN_CMD1, | 1051 return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, |
1064 data->fan_cmd1); 1065} 1066 1067static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) 1068{ | 1052 data->fan_cmd1); 1053} 1054 1055static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id) 1056{ |
1057 struct device *dev = &client->dev; |
|
1069 struct g762_data *data; 1070 int ret; 1071 1072 if (!i2c_check_functionality(client->adapter, 1073 I2C_FUNC_SMBUS_BYTE_DATA)) 1074 return -ENODEV; 1075 | 1058 struct g762_data *data; 1059 int ret; 1060 1061 if (!i2c_check_functionality(client->adapter, 1062 I2C_FUNC_SMBUS_BYTE_DATA)) 1063 return -ENODEV; 1064 |
1076 data = devm_kzalloc(&client->dev, sizeof(struct g762_data), GFP_KERNEL); | 1065 data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL); |
1077 if (!data) 1078 return -ENOMEM; 1079 1080 i2c_set_clientdata(client, data); 1081 data->client = client; 1082 mutex_init(&data->update_lock); 1083 1084 /* Enable fan failure detection and fan out of control protection */ | 1066 if (!data) 1067 return -ENOMEM; 1068 1069 i2c_set_clientdata(client, data); 1070 data->client = client; 1071 mutex_init(&data->update_lock); 1072 1073 /* Enable fan failure detection and fan out of control protection */ |
1085 ret = g762_fan_init(&client->dev); | 1074 ret = g762_fan_init(dev); |
1086 if (ret) 1087 return ret; 1088 1089 /* Get configuration via DT ... */ 1090 ret = g762_of_clock_enable(client); 1091 if (ret) 1092 return ret; 1093 ret = g762_of_prop_import(client); 1094 if (ret) 1095 goto clock_dis; 1096 /* ... or platform_data */ 1097 ret = g762_pdata_prop_import(client); 1098 if (ret) 1099 goto clock_dis; 1100 | 1075 if (ret) 1076 return ret; 1077 1078 /* Get configuration via DT ... */ 1079 ret = g762_of_clock_enable(client); 1080 if (ret) 1081 return ret; 1082 ret = g762_of_prop_import(client); 1083 if (ret) 1084 goto clock_dis; 1085 /* ... or platform_data */ 1086 ret = g762_pdata_prop_import(client); 1087 if (ret) 1088 goto clock_dis; 1089 |
1101 /* Register sysfs hooks */ 1102 ret = sysfs_create_group(&client->dev.kobj, &g762_group); 1103 if (ret) 1104 goto clock_dis; 1105 1106 data->hwmon_dev = hwmon_device_register(&client->dev); | 1090 data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, 1091 client->name, 1092 data, 1093 g762_groups); |
1107 if (IS_ERR(data->hwmon_dev)) { 1108 ret = PTR_ERR(data->hwmon_dev); | 1094 if (IS_ERR(data->hwmon_dev)) { 1095 ret = PTR_ERR(data->hwmon_dev); |
1109 goto sysfs_rem; | 1096 goto clock_dis; |
1110 } 1111 1112 return 0; 1113 | 1097 } 1098 1099 return 0; 1100 |
1114 sysfs_rem: 1115 sysfs_remove_group(&client->dev.kobj, &g762_group); 1116 | |
1117 clock_dis: 1118 g762_of_clock_disable(client); 1119 1120 return ret; 1121} 1122 1123static int g762_remove(struct i2c_client *client) 1124{ 1125 struct g762_data *data = i2c_get_clientdata(client); 1126 1127 hwmon_device_unregister(data->hwmon_dev); | 1101 clock_dis: 1102 g762_of_clock_disable(client); 1103 1104 return ret; 1105} 1106 1107static int g762_remove(struct i2c_client *client) 1108{ 1109 struct g762_data *data = i2c_get_clientdata(client); 1110 1111 hwmon_device_unregister(data->hwmon_dev); |
1128 sysfs_remove_group(&client->dev.kobj, &g762_group); | |
1129 g762_of_clock_disable(client); 1130 1131 return 0; 1132} 1133 1134static struct i2c_driver g762_driver = { 1135 .driver = { 1136 .name = DRVNAME, --- 13 unchanged lines hidden --- | 1112 g762_of_clock_disable(client); 1113 1114 return 0; 1115} 1116 1117static struct i2c_driver g762_driver = { 1118 .driver = { 1119 .name = DRVNAME, --- 13 unchanged lines hidden --- |