gl518sm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * gl518sm.c - Part of lm_sensors, Linux kernel modules for hardware
  4. * monitoring
  5. * Copyright (C) 1998, 1999 Frodo Looijaard <[email protected]> and
  6. * Kyosti Malkki <[email protected]>
  7. * Copyright (C) 2004 Hong-Gunn Chew <[email protected]> and
  8. * Jean Delvare <[email protected]>
  9. *
  10. * Ported to Linux 2.6 by Hong-Gunn Chew with the help of Jean Delvare
  11. * and advice of Greg Kroah-Hartman.
  12. *
  13. * Notes about the port:
  14. * Release 0x00 of the GL518SM chipset doesn't support reading of in0,
  15. * in1 nor in2. The original driver had an ugly workaround to get them
  16. * anyway (changing limits and watching alarms trigger and wear off).
  17. * We did not keep that part of the original driver in the Linux 2.6
  18. * version, since it was making the driver significantly more complex
  19. * with no real benefit.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/i2c.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-sysfs.h>
  28. #include <linux/err.h>
  29. #include <linux/mutex.h>
  30. #include <linux/sysfs.h>
  31. /* Addresses to scan */
  32. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  33. enum chips { gl518sm_r00, gl518sm_r80 };
  34. /* Many GL518 constants specified below */
  35. /* The GL518 registers */
  36. #define GL518_REG_CHIP_ID 0x00
  37. #define GL518_REG_REVISION 0x01
  38. #define GL518_REG_VENDOR_ID 0x02
  39. #define GL518_REG_CONF 0x03
  40. #define GL518_REG_TEMP_IN 0x04
  41. #define GL518_REG_TEMP_MAX 0x05
  42. #define GL518_REG_TEMP_HYST 0x06
  43. #define GL518_REG_FAN_COUNT 0x07
  44. #define GL518_REG_FAN_LIMIT 0x08
  45. #define GL518_REG_VIN1_LIMIT 0x09
  46. #define GL518_REG_VIN2_LIMIT 0x0a
  47. #define GL518_REG_VIN3_LIMIT 0x0b
  48. #define GL518_REG_VDD_LIMIT 0x0c
  49. #define GL518_REG_VIN3 0x0d
  50. #define GL518_REG_MISC 0x0f
  51. #define GL518_REG_ALARM 0x10
  52. #define GL518_REG_MASK 0x11
  53. #define GL518_REG_INT 0x12
  54. #define GL518_REG_VIN2 0x13
  55. #define GL518_REG_VIN1 0x14
  56. #define GL518_REG_VDD 0x15
  57. /*
  58. * Conversions. Rounding and limit checking is only done on the TO_REG
  59. * variants. Note that you should be a bit careful with which arguments
  60. * these macros are called: arguments may be evaluated more than once.
  61. * Fixing this is just not worth it.
  62. */
  63. #define RAW_FROM_REG(val) val
  64. #define BOOL_FROM_REG(val) ((val) ? 0 : 1)
  65. #define BOOL_TO_REG(val) ((val) ? 0 : 1)
  66. #define TEMP_CLAMP(val) clamp_val(val, -119000, 136000)
  67. #define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(TEMP_CLAMP(val), 1000) + 119)
  68. #define TEMP_FROM_REG(val) (((val) - 119) * 1000)
  69. static inline u8 FAN_TO_REG(long rpm, int div)
  70. {
  71. long rpmdiv;
  72. if (rpm == 0)
  73. return 0;
  74. rpmdiv = clamp_val(rpm, 1, 960000) * div;
  75. return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255);
  76. }
  77. #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : (480000 / ((val) * (div))))
  78. #define IN_CLAMP(val) clamp_val(val, 0, 255 * 19)
  79. #define IN_TO_REG(val) DIV_ROUND_CLOSEST(IN_CLAMP(val), 19)
  80. #define IN_FROM_REG(val) ((val) * 19)
  81. #define VDD_CLAMP(val) clamp_val(val, 0, 255 * 95 / 4)
  82. #define VDD_TO_REG(val) DIV_ROUND_CLOSEST(VDD_CLAMP(val) * 4, 95)
  83. #define VDD_FROM_REG(val) DIV_ROUND_CLOSEST((val) * 95, 4)
  84. #define DIV_FROM_REG(val) (1 << (val))
  85. #define BEEP_MASK_TO_REG(val) ((val) & 0x7f & data->alarm_mask)
  86. #define BEEP_MASK_FROM_REG(val) ((val) & 0x7f)
  87. /* Each client has this additional data */
  88. struct gl518_data {
  89. struct i2c_client *client;
  90. const struct attribute_group *groups[3];
  91. enum chips type;
  92. struct mutex update_lock;
  93. bool valid; /* true if following fields are valid */
  94. unsigned long last_updated; /* In jiffies */
  95. u8 voltage_in[4]; /* Register values; [0] = VDD */
  96. u8 voltage_min[4]; /* Register values; [0] = VDD */
  97. u8 voltage_max[4]; /* Register values; [0] = VDD */
  98. u8 fan_in[2];
  99. u8 fan_min[2];
  100. u8 fan_div[2]; /* Register encoding, shifted right */
  101. u8 fan_auto1; /* Boolean */
  102. u8 temp_in; /* Register values */
  103. u8 temp_max; /* Register values */
  104. u8 temp_hyst; /* Register values */
  105. u8 alarms; /* Register value */
  106. u8 alarm_mask;
  107. u8 beep_mask; /* Register value */
  108. u8 beep_enable; /* Boolean */
  109. };
  110. /*
  111. * Registers 0x07 to 0x0c are word-sized, others are byte-sized
  112. * GL518 uses a high-byte first convention, which is exactly opposite to
  113. * the SMBus standard.
  114. */
  115. static int gl518_read_value(struct i2c_client *client, u8 reg)
  116. {
  117. if ((reg >= 0x07) && (reg <= 0x0c))
  118. return i2c_smbus_read_word_swapped(client, reg);
  119. else
  120. return i2c_smbus_read_byte_data(client, reg);
  121. }
  122. static int gl518_write_value(struct i2c_client *client, u8 reg, u16 value)
  123. {
  124. if ((reg >= 0x07) && (reg <= 0x0c))
  125. return i2c_smbus_write_word_swapped(client, reg, value);
  126. else
  127. return i2c_smbus_write_byte_data(client, reg, value);
  128. }
  129. static struct gl518_data *gl518_update_device(struct device *dev)
  130. {
  131. struct gl518_data *data = dev_get_drvdata(dev);
  132. struct i2c_client *client = data->client;
  133. int val;
  134. mutex_lock(&data->update_lock);
  135. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  136. || !data->valid) {
  137. dev_dbg(&client->dev, "Starting gl518 update\n");
  138. data->alarms = gl518_read_value(client, GL518_REG_INT);
  139. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  140. val = gl518_read_value(client, GL518_REG_VDD_LIMIT);
  141. data->voltage_min[0] = val & 0xff;
  142. data->voltage_max[0] = (val >> 8) & 0xff;
  143. val = gl518_read_value(client, GL518_REG_VIN1_LIMIT);
  144. data->voltage_min[1] = val & 0xff;
  145. data->voltage_max[1] = (val >> 8) & 0xff;
  146. val = gl518_read_value(client, GL518_REG_VIN2_LIMIT);
  147. data->voltage_min[2] = val & 0xff;
  148. data->voltage_max[2] = (val >> 8) & 0xff;
  149. val = gl518_read_value(client, GL518_REG_VIN3_LIMIT);
  150. data->voltage_min[3] = val & 0xff;
  151. data->voltage_max[3] = (val >> 8) & 0xff;
  152. val = gl518_read_value(client, GL518_REG_FAN_COUNT);
  153. data->fan_in[0] = (val >> 8) & 0xff;
  154. data->fan_in[1] = val & 0xff;
  155. val = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  156. data->fan_min[0] = (val >> 8) & 0xff;
  157. data->fan_min[1] = val & 0xff;
  158. data->temp_in = gl518_read_value(client, GL518_REG_TEMP_IN);
  159. data->temp_max =
  160. gl518_read_value(client, GL518_REG_TEMP_MAX);
  161. data->temp_hyst =
  162. gl518_read_value(client, GL518_REG_TEMP_HYST);
  163. val = gl518_read_value(client, GL518_REG_MISC);
  164. data->fan_div[0] = (val >> 6) & 0x03;
  165. data->fan_div[1] = (val >> 4) & 0x03;
  166. data->fan_auto1 = (val >> 3) & 0x01;
  167. data->alarms &= data->alarm_mask;
  168. val = gl518_read_value(client, GL518_REG_CONF);
  169. data->beep_enable = (val >> 2) & 1;
  170. if (data->type != gl518sm_r00) {
  171. data->voltage_in[0] =
  172. gl518_read_value(client, GL518_REG_VDD);
  173. data->voltage_in[1] =
  174. gl518_read_value(client, GL518_REG_VIN1);
  175. data->voltage_in[2] =
  176. gl518_read_value(client, GL518_REG_VIN2);
  177. }
  178. data->voltage_in[3] =
  179. gl518_read_value(client, GL518_REG_VIN3);
  180. data->last_updated = jiffies;
  181. data->valid = true;
  182. }
  183. mutex_unlock(&data->update_lock);
  184. return data;
  185. }
  186. /*
  187. * Sysfs stuff
  188. */
  189. #define show(type, suffix, value) \
  190. static ssize_t show_##suffix(struct device *dev, \
  191. struct device_attribute *attr, char *buf) \
  192. { \
  193. struct gl518_data *data = gl518_update_device(dev); \
  194. return sprintf(buf, "%d\n", type##_FROM_REG(data->value)); \
  195. }
  196. show(TEMP, temp_input1, temp_in);
  197. show(TEMP, temp_max1, temp_max);
  198. show(TEMP, temp_hyst1, temp_hyst);
  199. show(BOOL, fan_auto1, fan_auto1);
  200. show(VDD, in_input0, voltage_in[0]);
  201. show(IN, in_input1, voltage_in[1]);
  202. show(IN, in_input2, voltage_in[2]);
  203. show(IN, in_input3, voltage_in[3]);
  204. show(VDD, in_min0, voltage_min[0]);
  205. show(IN, in_min1, voltage_min[1]);
  206. show(IN, in_min2, voltage_min[2]);
  207. show(IN, in_min3, voltage_min[3]);
  208. show(VDD, in_max0, voltage_max[0]);
  209. show(IN, in_max1, voltage_max[1]);
  210. show(IN, in_max2, voltage_max[2]);
  211. show(IN, in_max3, voltage_max[3]);
  212. show(RAW, alarms, alarms);
  213. show(BOOL, beep_enable, beep_enable);
  214. show(BEEP_MASK, beep_mask, beep_mask);
  215. static ssize_t fan_input_show(struct device *dev,
  216. struct device_attribute *attr, char *buf)
  217. {
  218. int nr = to_sensor_dev_attr(attr)->index;
  219. struct gl518_data *data = gl518_update_device(dev);
  220. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_in[nr],
  221. DIV_FROM_REG(data->fan_div[nr])));
  222. }
  223. static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
  224. char *buf)
  225. {
  226. int nr = to_sensor_dev_attr(attr)->index;
  227. struct gl518_data *data = gl518_update_device(dev);
  228. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  229. DIV_FROM_REG(data->fan_div[nr])));
  230. }
  231. static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
  232. char *buf)
  233. {
  234. int nr = to_sensor_dev_attr(attr)->index;
  235. struct gl518_data *data = gl518_update_device(dev);
  236. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  237. }
  238. #define set(type, suffix, value, reg) \
  239. static ssize_t set_##suffix(struct device *dev, \
  240. struct device_attribute *attr, \
  241. const char *buf, size_t count) \
  242. { \
  243. struct gl518_data *data = dev_get_drvdata(dev); \
  244. struct i2c_client *client = data->client; \
  245. long val; \
  246. int err = kstrtol(buf, 10, &val); \
  247. if (err) \
  248. return err; \
  249. \
  250. mutex_lock(&data->update_lock); \
  251. data->value = type##_TO_REG(val); \
  252. gl518_write_value(client, reg, data->value); \
  253. mutex_unlock(&data->update_lock); \
  254. return count; \
  255. }
  256. #define set_bits(type, suffix, value, reg, mask, shift) \
  257. static ssize_t set_##suffix(struct device *dev, \
  258. struct device_attribute *attr, \
  259. const char *buf, size_t count) \
  260. { \
  261. struct gl518_data *data = dev_get_drvdata(dev); \
  262. struct i2c_client *client = data->client; \
  263. int regvalue; \
  264. unsigned long val; \
  265. int err = kstrtoul(buf, 10, &val); \
  266. if (err) \
  267. return err; \
  268. \
  269. mutex_lock(&data->update_lock); \
  270. regvalue = gl518_read_value(client, reg); \
  271. data->value = type##_TO_REG(val); \
  272. regvalue = (regvalue & ~mask) | (data->value << shift); \
  273. gl518_write_value(client, reg, regvalue); \
  274. mutex_unlock(&data->update_lock); \
  275. return count; \
  276. }
  277. #define set_low(type, suffix, value, reg) \
  278. set_bits(type, suffix, value, reg, 0x00ff, 0)
  279. #define set_high(type, suffix, value, reg) \
  280. set_bits(type, suffix, value, reg, 0xff00, 8)
  281. set(TEMP, temp_max1, temp_max, GL518_REG_TEMP_MAX);
  282. set(TEMP, temp_hyst1, temp_hyst, GL518_REG_TEMP_HYST);
  283. set_bits(BOOL, fan_auto1, fan_auto1, GL518_REG_MISC, 0x08, 3);
  284. set_low(VDD, in_min0, voltage_min[0], GL518_REG_VDD_LIMIT);
  285. set_low(IN, in_min1, voltage_min[1], GL518_REG_VIN1_LIMIT);
  286. set_low(IN, in_min2, voltage_min[2], GL518_REG_VIN2_LIMIT);
  287. set_low(IN, in_min3, voltage_min[3], GL518_REG_VIN3_LIMIT);
  288. set_high(VDD, in_max0, voltage_max[0], GL518_REG_VDD_LIMIT);
  289. set_high(IN, in_max1, voltage_max[1], GL518_REG_VIN1_LIMIT);
  290. set_high(IN, in_max2, voltage_max[2], GL518_REG_VIN2_LIMIT);
  291. set_high(IN, in_max3, voltage_max[3], GL518_REG_VIN3_LIMIT);
  292. set_bits(BOOL, beep_enable, beep_enable, GL518_REG_CONF, 0x04, 2);
  293. set(BEEP_MASK, beep_mask, beep_mask, GL518_REG_ALARM);
  294. static ssize_t fan_min_store(struct device *dev,
  295. struct device_attribute *attr, const char *buf,
  296. size_t count)
  297. {
  298. struct gl518_data *data = dev_get_drvdata(dev);
  299. struct i2c_client *client = data->client;
  300. int nr = to_sensor_dev_attr(attr)->index;
  301. int regvalue;
  302. unsigned long val;
  303. int err;
  304. err = kstrtoul(buf, 10, &val);
  305. if (err)
  306. return err;
  307. mutex_lock(&data->update_lock);
  308. regvalue = gl518_read_value(client, GL518_REG_FAN_LIMIT);
  309. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  310. regvalue = (regvalue & (0xff << (8 * nr)))
  311. | (data->fan_min[nr] << (8 * (1 - nr)));
  312. gl518_write_value(client, GL518_REG_FAN_LIMIT, regvalue);
  313. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  314. if (data->fan_min[nr] == 0)
  315. data->alarm_mask &= ~(0x20 << nr);
  316. else
  317. data->alarm_mask |= (0x20 << nr);
  318. data->beep_mask &= data->alarm_mask;
  319. gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
  320. mutex_unlock(&data->update_lock);
  321. return count;
  322. }
  323. static ssize_t fan_div_store(struct device *dev,
  324. struct device_attribute *attr, const char *buf,
  325. size_t count)
  326. {
  327. struct gl518_data *data = dev_get_drvdata(dev);
  328. struct i2c_client *client = data->client;
  329. int nr = to_sensor_dev_attr(attr)->index;
  330. int regvalue;
  331. unsigned long val;
  332. int err;
  333. err = kstrtoul(buf, 10, &val);
  334. if (err)
  335. return err;
  336. switch (val) {
  337. case 1:
  338. val = 0;
  339. break;
  340. case 2:
  341. val = 1;
  342. break;
  343. case 4:
  344. val = 2;
  345. break;
  346. case 8:
  347. val = 3;
  348. break;
  349. default:
  350. dev_err(dev,
  351. "Invalid fan clock divider %lu, choose one of 1, 2, 4 or 8\n",
  352. val);
  353. return -EINVAL;
  354. }
  355. mutex_lock(&data->update_lock);
  356. regvalue = gl518_read_value(client, GL518_REG_MISC);
  357. data->fan_div[nr] = val;
  358. regvalue = (regvalue & ~(0xc0 >> (2 * nr)))
  359. | (data->fan_div[nr] << (6 - 2 * nr));
  360. gl518_write_value(client, GL518_REG_MISC, regvalue);
  361. mutex_unlock(&data->update_lock);
  362. return count;
  363. }
  364. static DEVICE_ATTR(temp1_input, 0444, show_temp_input1, NULL);
  365. static DEVICE_ATTR(temp1_max, 0644, show_temp_max1, set_temp_max1);
  366. static DEVICE_ATTR(temp1_max_hyst, 0644,
  367. show_temp_hyst1, set_temp_hyst1);
  368. static DEVICE_ATTR(fan1_auto, 0644, show_fan_auto1, set_fan_auto1);
  369. static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
  370. static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
  371. static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
  372. static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
  373. static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
  374. static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
  375. static DEVICE_ATTR(in0_input, 0444, show_in_input0, NULL);
  376. static DEVICE_ATTR(in1_input, 0444, show_in_input1, NULL);
  377. static DEVICE_ATTR(in2_input, 0444, show_in_input2, NULL);
  378. static DEVICE_ATTR(in3_input, 0444, show_in_input3, NULL);
  379. static DEVICE_ATTR(in0_min, 0644, show_in_min0, set_in_min0);
  380. static DEVICE_ATTR(in1_min, 0644, show_in_min1, set_in_min1);
  381. static DEVICE_ATTR(in2_min, 0644, show_in_min2, set_in_min2);
  382. static DEVICE_ATTR(in3_min, 0644, show_in_min3, set_in_min3);
  383. static DEVICE_ATTR(in0_max, 0644, show_in_max0, set_in_max0);
  384. static DEVICE_ATTR(in1_max, 0644, show_in_max1, set_in_max1);
  385. static DEVICE_ATTR(in2_max, 0644, show_in_max2, set_in_max2);
  386. static DEVICE_ATTR(in3_max, 0644, show_in_max3, set_in_max3);
  387. static DEVICE_ATTR(alarms, 0444, show_alarms, NULL);
  388. static DEVICE_ATTR(beep_enable, 0644,
  389. show_beep_enable, set_beep_enable);
  390. static DEVICE_ATTR(beep_mask, 0644,
  391. show_beep_mask, set_beep_mask);
  392. static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
  393. char *buf)
  394. {
  395. int bitnr = to_sensor_dev_attr(attr)->index;
  396. struct gl518_data *data = gl518_update_device(dev);
  397. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  398. }
  399. static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
  400. static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
  401. static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
  402. static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
  403. static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 4);
  404. static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 5);
  405. static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 6);
  406. static ssize_t beep_show(struct device *dev, struct device_attribute *attr,
  407. char *buf)
  408. {
  409. int bitnr = to_sensor_dev_attr(attr)->index;
  410. struct gl518_data *data = gl518_update_device(dev);
  411. return sprintf(buf, "%u\n", (data->beep_mask >> bitnr) & 1);
  412. }
  413. static ssize_t beep_store(struct device *dev, struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. struct gl518_data *data = dev_get_drvdata(dev);
  417. struct i2c_client *client = data->client;
  418. int bitnr = to_sensor_dev_attr(attr)->index;
  419. unsigned long bit;
  420. int err;
  421. err = kstrtoul(buf, 10, &bit);
  422. if (err)
  423. return err;
  424. if (bit & ~1)
  425. return -EINVAL;
  426. mutex_lock(&data->update_lock);
  427. data->beep_mask = gl518_read_value(client, GL518_REG_ALARM);
  428. if (bit)
  429. data->beep_mask |= (1 << bitnr);
  430. else
  431. data->beep_mask &= ~(1 << bitnr);
  432. gl518_write_value(client, GL518_REG_ALARM, data->beep_mask);
  433. mutex_unlock(&data->update_lock);
  434. return count;
  435. }
  436. static SENSOR_DEVICE_ATTR_RW(in0_beep, beep, 0);
  437. static SENSOR_DEVICE_ATTR_RW(in1_beep, beep, 1);
  438. static SENSOR_DEVICE_ATTR_RW(in2_beep, beep, 2);
  439. static SENSOR_DEVICE_ATTR_RW(in3_beep, beep, 3);
  440. static SENSOR_DEVICE_ATTR_RW(temp1_beep, beep, 4);
  441. static SENSOR_DEVICE_ATTR_RW(fan1_beep, beep, 5);
  442. static SENSOR_DEVICE_ATTR_RW(fan2_beep, beep, 6);
  443. static struct attribute *gl518_attributes[] = {
  444. &dev_attr_in3_input.attr,
  445. &dev_attr_in0_min.attr,
  446. &dev_attr_in1_min.attr,
  447. &dev_attr_in2_min.attr,
  448. &dev_attr_in3_min.attr,
  449. &dev_attr_in0_max.attr,
  450. &dev_attr_in1_max.attr,
  451. &dev_attr_in2_max.attr,
  452. &dev_attr_in3_max.attr,
  453. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  454. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  455. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  456. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  457. &sensor_dev_attr_in0_beep.dev_attr.attr,
  458. &sensor_dev_attr_in1_beep.dev_attr.attr,
  459. &sensor_dev_attr_in2_beep.dev_attr.attr,
  460. &sensor_dev_attr_in3_beep.dev_attr.attr,
  461. &dev_attr_fan1_auto.attr,
  462. &sensor_dev_attr_fan1_input.dev_attr.attr,
  463. &sensor_dev_attr_fan2_input.dev_attr.attr,
  464. &sensor_dev_attr_fan1_min.dev_attr.attr,
  465. &sensor_dev_attr_fan2_min.dev_attr.attr,
  466. &sensor_dev_attr_fan1_div.dev_attr.attr,
  467. &sensor_dev_attr_fan2_div.dev_attr.attr,
  468. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  469. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  470. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  471. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  472. &dev_attr_temp1_input.attr,
  473. &dev_attr_temp1_max.attr,
  474. &dev_attr_temp1_max_hyst.attr,
  475. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  476. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  477. &dev_attr_alarms.attr,
  478. &dev_attr_beep_enable.attr,
  479. &dev_attr_beep_mask.attr,
  480. NULL
  481. };
  482. static const struct attribute_group gl518_group = {
  483. .attrs = gl518_attributes,
  484. };
  485. static struct attribute *gl518_attributes_r80[] = {
  486. &dev_attr_in0_input.attr,
  487. &dev_attr_in1_input.attr,
  488. &dev_attr_in2_input.attr,
  489. NULL
  490. };
  491. static const struct attribute_group gl518_group_r80 = {
  492. .attrs = gl518_attributes_r80,
  493. };
  494. /*
  495. * Real code
  496. */
  497. /* Return 0 if detection is successful, -ENODEV otherwise */
  498. static int gl518_detect(struct i2c_client *client, struct i2c_board_info *info)
  499. {
  500. struct i2c_adapter *adapter = client->adapter;
  501. int rev;
  502. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  503. I2C_FUNC_SMBUS_WORD_DATA))
  504. return -ENODEV;
  505. /* Now, we do the remaining detection. */
  506. if ((gl518_read_value(client, GL518_REG_CHIP_ID) != 0x80)
  507. || (gl518_read_value(client, GL518_REG_CONF) & 0x80))
  508. return -ENODEV;
  509. /* Determine the chip type. */
  510. rev = gl518_read_value(client, GL518_REG_REVISION);
  511. if (rev != 0x00 && rev != 0x80)
  512. return -ENODEV;
  513. strscpy(info->type, "gl518sm", I2C_NAME_SIZE);
  514. return 0;
  515. }
  516. /*
  517. * Called when we have found a new GL518SM.
  518. * Note that we preserve D4:NoFan2 and D2:beep_enable.
  519. */
  520. static void gl518_init_client(struct i2c_client *client)
  521. {
  522. /* Make sure we leave D7:Reset untouched */
  523. u8 regvalue = gl518_read_value(client, GL518_REG_CONF) & 0x7f;
  524. /* Comparator mode (D3=0), standby mode (D6=0) */
  525. gl518_write_value(client, GL518_REG_CONF, (regvalue &= 0x37));
  526. /* Never interrupts */
  527. gl518_write_value(client, GL518_REG_MASK, 0x00);
  528. /* Clear status register (D5=1), start (D6=1) */
  529. gl518_write_value(client, GL518_REG_CONF, 0x20 | regvalue);
  530. gl518_write_value(client, GL518_REG_CONF, 0x40 | regvalue);
  531. }
  532. static int gl518_probe(struct i2c_client *client)
  533. {
  534. struct device *dev = &client->dev;
  535. struct device *hwmon_dev;
  536. struct gl518_data *data;
  537. int revision;
  538. data = devm_kzalloc(dev, sizeof(struct gl518_data), GFP_KERNEL);
  539. if (!data)
  540. return -ENOMEM;
  541. data->client = client;
  542. revision = gl518_read_value(client, GL518_REG_REVISION);
  543. data->type = revision == 0x80 ? gl518sm_r80 : gl518sm_r00;
  544. mutex_init(&data->update_lock);
  545. /* Initialize the GL518SM chip */
  546. data->alarm_mask = 0xff;
  547. gl518_init_client(client);
  548. /* sysfs hooks */
  549. data->groups[0] = &gl518_group;
  550. if (data->type == gl518sm_r80)
  551. data->groups[1] = &gl518_group_r80;
  552. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  553. data, data->groups);
  554. return PTR_ERR_OR_ZERO(hwmon_dev);
  555. }
  556. static const struct i2c_device_id gl518_id[] = {
  557. { "gl518sm", 0 },
  558. { }
  559. };
  560. MODULE_DEVICE_TABLE(i2c, gl518_id);
  561. static struct i2c_driver gl518_driver = {
  562. .class = I2C_CLASS_HWMON,
  563. .driver = {
  564. .name = "gl518sm",
  565. },
  566. .probe_new = gl518_probe,
  567. .id_table = gl518_id,
  568. .detect = gl518_detect,
  569. .address_list = normal_i2c,
  570. };
  571. module_i2c_driver(gl518_driver);
  572. MODULE_AUTHOR("Frodo Looijaard <[email protected]>, "
  573. "Kyosti Malkki <[email protected]> and "
  574. "Hong-Gunn Chew <[email protected]>");
  575. MODULE_DESCRIPTION("GL518SM driver");
  576. MODULE_LICENSE("GPL");