g760a.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * g760a - Driver for the Global Mixed-mode Technology Inc. G760A
  3. * fan speed PWM controller chip
  4. *
  5. * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
  6. *
  7. * Complete datasheet is available at GMT's website:
  8. * http://www.gmt.com.tw/product/datasheet/EDS-760A.pdf
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. #include <linux/mutex.h>
  24. #include <linux/sysfs.h>
  25. enum g760a_regs {
  26. G760A_REG_SET_CNT = 0x00,
  27. G760A_REG_ACT_CNT = 0x01,
  28. G760A_REG_FAN_STA = 0x02
  29. };
  30. #define G760A_REG_FAN_STA_RPM_OFF 0x1 /* +/-20% off */
  31. #define G760A_REG_FAN_STA_RPM_LOW 0x2 /* below 1920rpm */
  32. /* register data is read (and cached) at most once per second */
  33. #define G760A_UPDATE_INTERVAL (HZ)
  34. struct g760a_data {
  35. struct i2c_client *client;
  36. struct mutex update_lock;
  37. /* board specific parameters */
  38. u32 clk; /* default 32kHz */
  39. u16 fan_div; /* default P=2 */
  40. /* g760a register cache */
  41. unsigned int valid:1;
  42. unsigned long last_updated; /* In jiffies */
  43. u8 set_cnt; /* PWM (period) count number; 0xff stops fan */
  44. u8 act_cnt; /* formula: cnt = (CLK * 30)/(rpm * P) */
  45. u8 fan_sta; /* bit 0: set when actual fan speed more than 20%
  46. * outside requested fan speed
  47. * bit 1: set when fan speed below 1920 rpm
  48. */
  49. };
  50. #define G760A_DEFAULT_CLK 32768
  51. #define G760A_DEFAULT_FAN_DIV 2
  52. #define PWM_FROM_CNT(cnt) (0xff-(cnt))
  53. #define PWM_TO_CNT(pwm) (0xff-(pwm))
  54. static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
  55. {
  56. return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
  57. }
  58. /* read/write wrappers */
  59. static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
  60. {
  61. return i2c_smbus_read_byte_data(client, reg);
  62. }
  63. static int g760a_write_value(struct i2c_client *client, enum g760a_regs reg,
  64. u16 value)
  65. {
  66. return i2c_smbus_write_byte_data(client, reg, value);
  67. }
  68. /*
  69. * sysfs attributes
  70. */
  71. static struct g760a_data *g760a_update_client(struct device *dev)
  72. {
  73. struct g760a_data *data = dev_get_drvdata(dev);
  74. struct i2c_client *client = data->client;
  75. mutex_lock(&data->update_lock);
  76. if (time_after(jiffies, data->last_updated + G760A_UPDATE_INTERVAL)
  77. || !data->valid) {
  78. dev_dbg(&client->dev, "Starting g760a update\n");
  79. data->set_cnt = g760a_read_value(client, G760A_REG_SET_CNT);
  80. data->act_cnt = g760a_read_value(client, G760A_REG_ACT_CNT);
  81. data->fan_sta = g760a_read_value(client, G760A_REG_FAN_STA);
  82. data->last_updated = jiffies;
  83. data->valid = 1;
  84. }
  85. mutex_unlock(&data->update_lock);
  86. return data;
  87. }
  88. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  89. char *buf)
  90. {
  91. struct g760a_data *data = g760a_update_client(dev);
  92. unsigned int rpm = 0;
  93. mutex_lock(&data->update_lock);
  94. if (!(data->fan_sta & G760A_REG_FAN_STA_RPM_LOW))
  95. rpm = rpm_from_cnt(data->act_cnt, data->clk, data->fan_div);
  96. mutex_unlock(&data->update_lock);
  97. return sprintf(buf, "%d\n", rpm);
  98. }
  99. static ssize_t show_fan_alarm(struct device *dev, struct device_attribute *da,
  100. char *buf)
  101. {
  102. struct g760a_data *data = g760a_update_client(dev);
  103. int fan_alarm = (data->fan_sta & G760A_REG_FAN_STA_RPM_OFF) ? 1 : 0;
  104. return sprintf(buf, "%d\n", fan_alarm);
  105. }
  106. static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
  107. char *buf)
  108. {
  109. struct g760a_data *data = g760a_update_client(dev);
  110. return sprintf(buf, "%d\n", PWM_FROM_CNT(data->set_cnt));
  111. }
  112. static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
  113. const char *buf, size_t count)
  114. {
  115. struct g760a_data *data = g760a_update_client(dev);
  116. struct i2c_client *client = data->client;
  117. unsigned long val;
  118. if (kstrtoul(buf, 10, &val))
  119. return -EINVAL;
  120. mutex_lock(&data->update_lock);
  121. data->set_cnt = PWM_TO_CNT(clamp_val(val, 0, 255));
  122. g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
  123. mutex_unlock(&data->update_lock);
  124. return count;
  125. }
  126. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
  127. static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
  128. static DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL);
  129. static struct attribute *g760a_attrs[] = {
  130. &dev_attr_pwm1.attr,
  131. &dev_attr_fan1_input.attr,
  132. &dev_attr_fan1_alarm.attr,
  133. NULL
  134. };
  135. ATTRIBUTE_GROUPS(g760a);
  136. /*
  137. * new-style driver model code
  138. */
  139. static int g760a_probe(struct i2c_client *client,
  140. const struct i2c_device_id *id)
  141. {
  142. struct device *dev = &client->dev;
  143. struct g760a_data *data;
  144. struct device *hwmon_dev;
  145. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  146. return -EIO;
  147. data = devm_kzalloc(dev, sizeof(struct g760a_data), GFP_KERNEL);
  148. if (!data)
  149. return -ENOMEM;
  150. data->client = client;
  151. mutex_init(&data->update_lock);
  152. /* setup default configuration for now */
  153. data->fan_div = G760A_DEFAULT_FAN_DIV;
  154. data->clk = G760A_DEFAULT_CLK;
  155. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  156. data,
  157. g760a_groups);
  158. return PTR_ERR_OR_ZERO(hwmon_dev);
  159. }
  160. static const struct i2c_device_id g760a_id[] = {
  161. { "g760a", 0 },
  162. { }
  163. };
  164. MODULE_DEVICE_TABLE(i2c, g760a_id);
  165. static struct i2c_driver g760a_driver = {
  166. .driver = {
  167. .name = "g760a",
  168. },
  169. .probe = g760a_probe,
  170. .id_table = g760a_id,
  171. };
  172. module_i2c_driver(g760a_driver);
  173. MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
  174. MODULE_DESCRIPTION("GMT G760A driver");
  175. MODULE_LICENSE("GPL");