g760a.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. static const struct i2c_device_id g760a_id[] = {
  26. { "g760a", 0 },
  27. { }
  28. };
  29. MODULE_DEVICE_TABLE(i2c, g760a_id);
  30. enum g760a_regs {
  31. G760A_REG_SET_CNT = 0x00,
  32. G760A_REG_ACT_CNT = 0x01,
  33. G760A_REG_FAN_STA = 0x02
  34. };
  35. #define G760A_REG_FAN_STA_RPM_OFF 0x1 /* +/-20% off */
  36. #define G760A_REG_FAN_STA_RPM_LOW 0x2 /* below 1920rpm */
  37. /* register data is read (and cached) at most once per second */
  38. #define G760A_UPDATE_INTERVAL (HZ)
  39. struct g760a_data {
  40. struct i2c_client *client;
  41. struct device *hwmon_dev;
  42. struct mutex update_lock;
  43. /* board specific parameters */
  44. u32 clk; /* default 32kHz */
  45. u16 fan_div; /* default P=2 */
  46. /* g760a register cache */
  47. unsigned int valid:1;
  48. unsigned long last_updated; /* In jiffies */
  49. u8 set_cnt; /* PWM (period) count number; 0xff stops fan */
  50. u8 act_cnt; /* formula: cnt = (CLK * 30)/(rpm * P) */
  51. u8 fan_sta; /* bit 0: set when actual fan speed more than 20%
  52. * outside requested fan speed
  53. * bit 1: set when fan speed below 1920 rpm
  54. */
  55. };
  56. #define G760A_DEFAULT_CLK 32768
  57. #define G760A_DEFAULT_FAN_DIV 2
  58. #define PWM_FROM_CNT(cnt) (0xff-(cnt))
  59. #define PWM_TO_CNT(pwm) (0xff-(pwm))
  60. static inline unsigned int rpm_from_cnt(u8 val, u32 clk, u16 div)
  61. {
  62. return ((val == 0x00) ? 0 : ((clk*30)/(val*div)));
  63. }
  64. /* new-style driver model */
  65. static int g760a_probe(struct i2c_client *client,
  66. const struct i2c_device_id *id);
  67. static int g760a_remove(struct i2c_client *client);
  68. static struct i2c_driver g760a_driver = {
  69. .driver = {
  70. .name = "g760a",
  71. },
  72. .probe = g760a_probe,
  73. .remove = g760a_remove,
  74. .id_table = g760a_id,
  75. };
  76. /* read/write wrappers */
  77. static int g760a_read_value(struct i2c_client *client, enum g760a_regs reg)
  78. {
  79. return i2c_smbus_read_byte_data(client, reg);
  80. }
  81. static int g760a_write_value(struct i2c_client *client, enum g760a_regs reg,
  82. u16 value)
  83. {
  84. return i2c_smbus_write_byte_data(client, reg, value);
  85. }
  86. /*
  87. * sysfs attributes
  88. */
  89. static struct g760a_data *g760a_update_client(struct device *dev)
  90. {
  91. struct i2c_client *client = to_i2c_client(dev);
  92. struct g760a_data *data = i2c_get_clientdata(client);
  93. mutex_lock(&data->update_lock);
  94. if (time_after(jiffies, data->last_updated + G760A_UPDATE_INTERVAL)
  95. || !data->valid) {
  96. dev_dbg(&client->dev, "Starting g760a update\n");
  97. data->set_cnt = g760a_read_value(client, G760A_REG_SET_CNT);
  98. data->act_cnt = g760a_read_value(client, G760A_REG_ACT_CNT);
  99. data->fan_sta = g760a_read_value(client, G760A_REG_FAN_STA);
  100. data->last_updated = jiffies;
  101. data->valid = 1;
  102. }
  103. mutex_unlock(&data->update_lock);
  104. return data;
  105. }
  106. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  107. char *buf)
  108. {
  109. struct g760a_data *data = g760a_update_client(dev);
  110. unsigned int rpm = 0;
  111. mutex_lock(&data->update_lock);
  112. if (!(data->fan_sta & G760A_REG_FAN_STA_RPM_LOW))
  113. rpm = rpm_from_cnt(data->act_cnt, data->clk, data->fan_div);
  114. mutex_unlock(&data->update_lock);
  115. return sprintf(buf, "%d\n", rpm);
  116. }
  117. static ssize_t show_fan_alarm(struct device *dev, struct device_attribute *da,
  118. char *buf)
  119. {
  120. struct g760a_data *data = g760a_update_client(dev);
  121. int fan_alarm = (data->fan_sta & G760A_REG_FAN_STA_RPM_OFF) ? 1 : 0;
  122. return sprintf(buf, "%d\n", fan_alarm);
  123. }
  124. static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
  125. char *buf)
  126. {
  127. struct g760a_data *data = g760a_update_client(dev);
  128. return sprintf(buf, "%d\n", PWM_FROM_CNT(data->set_cnt));
  129. }
  130. static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
  131. const char *buf, size_t count)
  132. {
  133. struct i2c_client *client = to_i2c_client(dev);
  134. struct g760a_data *data = g760a_update_client(dev);
  135. unsigned long val;
  136. if (kstrtoul(buf, 10, &val))
  137. return -EINVAL;
  138. mutex_lock(&data->update_lock);
  139. data->set_cnt = PWM_TO_CNT(SENSORS_LIMIT(val, 0, 255));
  140. g760a_write_value(client, G760A_REG_SET_CNT, data->set_cnt);
  141. mutex_unlock(&data->update_lock);
  142. return count;
  143. }
  144. static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
  145. static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
  146. static DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL);
  147. static struct attribute *g760a_attributes[] = {
  148. &dev_attr_pwm1.attr,
  149. &dev_attr_fan1_input.attr,
  150. &dev_attr_fan1_alarm.attr,
  151. NULL
  152. };
  153. static const struct attribute_group g760a_group = {
  154. .attrs = g760a_attributes,
  155. };
  156. /*
  157. * new-style driver model code
  158. */
  159. static int g760a_probe(struct i2c_client *client,
  160. const struct i2c_device_id *id)
  161. {
  162. struct g760a_data *data;
  163. int err;
  164. if (!i2c_check_functionality(client->adapter,
  165. I2C_FUNC_SMBUS_BYTE_DATA))
  166. return -EIO;
  167. data = kzalloc(sizeof(struct g760a_data), GFP_KERNEL);
  168. if (!data)
  169. return -ENOMEM;
  170. i2c_set_clientdata(client, data);
  171. data->client = client;
  172. mutex_init(&data->update_lock);
  173. /* setup default configuration for now */
  174. data->fan_div = G760A_DEFAULT_FAN_DIV;
  175. data->clk = G760A_DEFAULT_CLK;
  176. /* Register sysfs hooks */
  177. err = sysfs_create_group(&client->dev.kobj, &g760a_group);
  178. if (err)
  179. goto error_sysfs_create_group;
  180. data->hwmon_dev = hwmon_device_register(&client->dev);
  181. if (IS_ERR(data->hwmon_dev)) {
  182. err = PTR_ERR(data->hwmon_dev);
  183. goto error_hwmon_device_register;
  184. }
  185. return 0;
  186. error_hwmon_device_register:
  187. sysfs_remove_group(&client->dev.kobj, &g760a_group);
  188. error_sysfs_create_group:
  189. kfree(data);
  190. return err;
  191. }
  192. static int g760a_remove(struct i2c_client *client)
  193. {
  194. struct g760a_data *data = i2c_get_clientdata(client);
  195. hwmon_device_unregister(data->hwmon_dev);
  196. sysfs_remove_group(&client->dev.kobj, &g760a_group);
  197. kfree(data);
  198. return 0;
  199. }
  200. module_i2c_driver(g760a_driver);
  201. MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
  202. MODULE_DESCRIPTION("GMT G760A driver");
  203. MODULE_LICENSE("GPL");