gl520sm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. gl520sm.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>,
  5. Kyösti Mälkki <kmalkki@cc.hut.fi>
  6. Copyright (c) 2005 Maarten Deprez <maartendeprez@users.sourceforge.net>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/i2c-sensor.h>
  24. #include <linux/i2c-vid.h>
  25. /* Type of the extra sensor */
  26. static unsigned short extra_sensor_type;
  27. module_param(extra_sensor_type, ushort, 0);
  28. MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
  29. /* Addresses to scan */
  30. static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
  31. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  32. /* Insmod parameters */
  33. SENSORS_INSMOD_1(gl520sm);
  34. /* Many GL520 constants specified below
  35. One of the inputs can be configured as either temp or voltage.
  36. That's why _TEMP2 and _IN4 access the same register
  37. */
  38. /* The GL520 registers */
  39. #define GL520_REG_CHIP_ID 0x00
  40. #define GL520_REG_REVISION 0x01
  41. #define GL520_REG_CONF 0x03
  42. #define GL520_REG_MASK 0x11
  43. #define GL520_REG_VID_INPUT 0x02
  44. #define GL520_REG_IN0_INPUT 0x15
  45. #define GL520_REG_IN0_LIMIT 0x0c
  46. #define GL520_REG_IN0_MIN GL520_REG_IN0_LIMIT
  47. #define GL520_REG_IN0_MAX GL520_REG_IN0_LIMIT
  48. #define GL520_REG_IN1_INPUT 0x14
  49. #define GL520_REG_IN1_LIMIT 0x09
  50. #define GL520_REG_IN1_MIN GL520_REG_IN1_LIMIT
  51. #define GL520_REG_IN1_MAX GL520_REG_IN1_LIMIT
  52. #define GL520_REG_IN2_INPUT 0x13
  53. #define GL520_REG_IN2_LIMIT 0x0a
  54. #define GL520_REG_IN2_MIN GL520_REG_IN2_LIMIT
  55. #define GL520_REG_IN2_MAX GL520_REG_IN2_LIMIT
  56. #define GL520_REG_IN3_INPUT 0x0d
  57. #define GL520_REG_IN3_LIMIT 0x0b
  58. #define GL520_REG_IN3_MIN GL520_REG_IN3_LIMIT
  59. #define GL520_REG_IN3_MAX GL520_REG_IN3_LIMIT
  60. #define GL520_REG_IN4_INPUT 0x0e
  61. #define GL520_REG_IN4_MAX 0x17
  62. #define GL520_REG_IN4_MIN 0x18
  63. #define GL520_REG_TEMP1_INPUT 0x04
  64. #define GL520_REG_TEMP1_MAX 0x05
  65. #define GL520_REG_TEMP1_MAX_HYST 0x06
  66. #define GL520_REG_TEMP2_INPUT 0x0e
  67. #define GL520_REG_TEMP2_MAX 0x17
  68. #define GL520_REG_TEMP2_MAX_HYST 0x18
  69. #define GL520_REG_FAN_INPUT 0x07
  70. #define GL520_REG_FAN_MIN 0x08
  71. #define GL520_REG_FAN_DIV 0x0f
  72. #define GL520_REG_FAN_OFF GL520_REG_FAN_DIV
  73. #define GL520_REG_ALARMS 0x12
  74. #define GL520_REG_BEEP_MASK 0x10
  75. #define GL520_REG_BEEP_ENABLE GL520_REG_CONF
  76. /*
  77. * Function declarations
  78. */
  79. static int gl520_attach_adapter(struct i2c_adapter *adapter);
  80. static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
  81. static void gl520_init_client(struct i2c_client *client);
  82. static int gl520_detach_client(struct i2c_client *client);
  83. static int gl520_read_value(struct i2c_client *client, u8 reg);
  84. static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
  85. static struct gl520_data *gl520_update_device(struct device *dev);
  86. /* Driver data */
  87. static struct i2c_driver gl520_driver = {
  88. .owner = THIS_MODULE,
  89. .name = "gl520sm",
  90. .id = I2C_DRIVERID_GL520,
  91. .flags = I2C_DF_NOTIFY,
  92. .attach_adapter = gl520_attach_adapter,
  93. .detach_client = gl520_detach_client,
  94. };
  95. /* Client data */
  96. struct gl520_data {
  97. struct i2c_client client;
  98. struct semaphore update_lock;
  99. char valid; /* zero until the following fields are valid */
  100. unsigned long last_updated; /* in jiffies */
  101. u8 vid;
  102. u8 vrm;
  103. u8 in_input[5]; /* [0] = VVD */
  104. u8 in_min[5]; /* [0] = VDD */
  105. u8 in_max[5]; /* [0] = VDD */
  106. u8 fan_input[2];
  107. u8 fan_min[2];
  108. u8 fan_div[2];
  109. u8 fan_off;
  110. u8 temp_input[2];
  111. u8 temp_max[2];
  112. u8 temp_max_hyst[2];
  113. u8 alarms;
  114. u8 beep_enable;
  115. u8 beep_mask;
  116. u8 alarm_mask;
  117. u8 two_temps;
  118. };
  119. /*
  120. * Sysfs stuff
  121. */
  122. #define sysfs_r(type, n, item, reg) \
  123. static ssize_t get_##type##item (struct gl520_data *, char *, int); \
  124. static ssize_t get_##type##n##item (struct device *, struct device_attribute *attr, char *); \
  125. static ssize_t get_##type##n##item (struct device *dev, struct device_attribute *attr, char *buf) \
  126. { \
  127. struct gl520_data *data = gl520_update_device(dev); \
  128. return get_##type##item(data, buf, (n)); \
  129. }
  130. #define sysfs_w(type, n, item, reg) \
  131. static ssize_t set_##type##item (struct i2c_client *, struct gl520_data *, const char *, size_t, int, int); \
  132. static ssize_t set_##type##n##item (struct device *, struct device_attribute *attr, const char *, size_t); \
  133. static ssize_t set_##type##n##item (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  134. { \
  135. struct i2c_client *client = to_i2c_client(dev); \
  136. struct gl520_data *data = i2c_get_clientdata(client); \
  137. return set_##type##item(client, data, buf, count, (n), reg); \
  138. }
  139. #define sysfs_rw_n(type, n, item, reg) \
  140. sysfs_r(type, n, item, reg) \
  141. sysfs_w(type, n, item, reg) \
  142. static DEVICE_ATTR(type##n##item, S_IRUGO | S_IWUSR, get_##type##n##item, set_##type##n##item);
  143. #define sysfs_ro_n(type, n, item, reg) \
  144. sysfs_r(type, n, item, reg) \
  145. static DEVICE_ATTR(type##n##item, S_IRUGO, get_##type##n##item, NULL);
  146. #define sysfs_rw(type, item, reg) \
  147. sysfs_r(type, 0, item, reg) \
  148. sysfs_w(type, 0, item, reg) \
  149. static DEVICE_ATTR(type##item, S_IRUGO | S_IWUSR, get_##type##0##item, set_##type##0##item);
  150. #define sysfs_ro(type, item, reg) \
  151. sysfs_r(type, 0, item, reg) \
  152. static DEVICE_ATTR(type##item, S_IRUGO, get_##type##0##item, NULL);
  153. #define sysfs_vid(n) \
  154. sysfs_ro_n(cpu, n, _vid, GL520_REG_VID_INPUT)
  155. #define device_create_file_vid(client, n) \
  156. device_create_file(&client->dev, &dev_attr_cpu##n##_vid)
  157. #define sysfs_in(n) \
  158. sysfs_ro_n(in, n, _input, GL520_REG_IN##n##INPUT) \
  159. sysfs_rw_n(in, n, _min, GL520_REG_IN##n##_MIN) \
  160. sysfs_rw_n(in, n, _max, GL520_REG_IN##n##_MAX) \
  161. #define device_create_file_in(client, n) \
  162. ({device_create_file(&client->dev, &dev_attr_in##n##_input); \
  163. device_create_file(&client->dev, &dev_attr_in##n##_min); \
  164. device_create_file(&client->dev, &dev_attr_in##n##_max);})
  165. #define sysfs_fan(n) \
  166. sysfs_ro_n(fan, n, _input, GL520_REG_FAN_INPUT) \
  167. sysfs_rw_n(fan, n, _min, GL520_REG_FAN_MIN) \
  168. sysfs_rw_n(fan, n, _div, GL520_REG_FAN_DIV)
  169. #define device_create_file_fan(client, n) \
  170. ({device_create_file(&client->dev, &dev_attr_fan##n##_input); \
  171. device_create_file(&client->dev, &dev_attr_fan##n##_min); \
  172. device_create_file(&client->dev, &dev_attr_fan##n##_div);})
  173. #define sysfs_fan_off(n) \
  174. sysfs_rw_n(fan, n, _off, GL520_REG_FAN_OFF) \
  175. #define device_create_file_fan_off(client, n) \
  176. device_create_file(&client->dev, &dev_attr_fan##n##_off)
  177. #define sysfs_temp(n) \
  178. sysfs_ro_n(temp, n, _input, GL520_REG_TEMP##n##_INPUT) \
  179. sysfs_rw_n(temp, n, _max, GL520_REG_TEMP##n##_MAX) \
  180. sysfs_rw_n(temp, n, _max_hyst, GL520_REG_TEMP##n##_MAX_HYST)
  181. #define device_create_file_temp(client, n) \
  182. ({device_create_file(&client->dev, &dev_attr_temp##n##_input); \
  183. device_create_file(&client->dev, &dev_attr_temp##n##_max); \
  184. device_create_file(&client->dev, &dev_attr_temp##n##_max_hyst);})
  185. #define sysfs_alarms() \
  186. sysfs_ro(alarms, , GL520_REG_ALARMS) \
  187. sysfs_rw(beep_enable, , GL520_REG_BEEP_ENABLE) \
  188. sysfs_rw(beep_mask, , GL520_REG_BEEP_MASK)
  189. #define device_create_file_alarms(client) \
  190. ({device_create_file(&client->dev, &dev_attr_alarms); \
  191. device_create_file(&client->dev, &dev_attr_beep_enable); \
  192. device_create_file(&client->dev, &dev_attr_beep_mask);})
  193. sysfs_vid(0)
  194. sysfs_in(0)
  195. sysfs_in(1)
  196. sysfs_in(2)
  197. sysfs_in(3)
  198. sysfs_in(4)
  199. sysfs_fan(1)
  200. sysfs_fan(2)
  201. sysfs_fan_off(1)
  202. sysfs_temp(1)
  203. sysfs_temp(2)
  204. sysfs_alarms()
  205. static ssize_t get_cpu_vid(struct gl520_data *data, char *buf, int n)
  206. {
  207. return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
  208. }
  209. #define VDD_FROM_REG(val) (((val)*95+2)/4)
  210. #define VDD_TO_REG(val) (SENSORS_LIMIT((((val)*4+47)/95),0,255))
  211. #define IN_FROM_REG(val) ((val)*19)
  212. #define IN_TO_REG(val) (SENSORS_LIMIT((((val)+9)/19),0,255))
  213. static ssize_t get_in_input(struct gl520_data *data, char *buf, int n)
  214. {
  215. u8 r = data->in_input[n];
  216. if (n == 0)
  217. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  218. else
  219. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  220. }
  221. static ssize_t get_in_min(struct gl520_data *data, char *buf, int n)
  222. {
  223. u8 r = data->in_min[n];
  224. if (n == 0)
  225. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  226. else
  227. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  228. }
  229. static ssize_t get_in_max(struct gl520_data *data, char *buf, int n)
  230. {
  231. u8 r = data->in_max[n];
  232. if (n == 0)
  233. return sprintf(buf, "%d\n", VDD_FROM_REG(r));
  234. else
  235. return sprintf(buf, "%d\n", IN_FROM_REG(r));
  236. }
  237. static ssize_t set_in_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  238. {
  239. long v = simple_strtol(buf, NULL, 10);
  240. u8 r;
  241. down(&data->update_lock);
  242. if (n == 0)
  243. r = VDD_TO_REG(v);
  244. else
  245. r = IN_TO_REG(v);
  246. data->in_min[n] = r;
  247. if (n < 4)
  248. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
  249. else
  250. gl520_write_value(client, reg, r);
  251. up(&data->update_lock);
  252. return count;
  253. }
  254. static ssize_t set_in_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  255. {
  256. long v = simple_strtol(buf, NULL, 10);
  257. u8 r;
  258. if (n == 0)
  259. r = VDD_TO_REG(v);
  260. else
  261. r = IN_TO_REG(v);
  262. down(&data->update_lock);
  263. data->in_max[n] = r;
  264. if (n < 4)
  265. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
  266. else
  267. gl520_write_value(client, reg, r);
  268. up(&data->update_lock);
  269. return count;
  270. }
  271. #define DIV_FROM_REG(val) (1 << (val))
  272. #define FAN_FROM_REG(val,div) ((val)==0 ? 0 : (480000/((val) << (div))))
  273. #define FAN_TO_REG(val,div) ((val)<=0?0:SENSORS_LIMIT((480000 + ((val) << ((div)-1))) / ((val) << (div)), 1, 255));
  274. static ssize_t get_fan_input(struct gl520_data *data, char *buf, int n)
  275. {
  276. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_input[n - 1], data->fan_div[n - 1]));
  277. }
  278. static ssize_t get_fan_min(struct gl520_data *data, char *buf, int n)
  279. {
  280. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[n - 1], data->fan_div[n - 1]));
  281. }
  282. static ssize_t get_fan_div(struct gl520_data *data, char *buf, int n)
  283. {
  284. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[n - 1]));
  285. }
  286. static ssize_t get_fan_off(struct gl520_data *data, char *buf, int n)
  287. {
  288. return sprintf(buf, "%d\n", data->fan_off);
  289. }
  290. static ssize_t set_fan_min(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  291. {
  292. unsigned long v = simple_strtoul(buf, NULL, 10);
  293. u8 r;
  294. down(&data->update_lock);
  295. r = FAN_TO_REG(v, data->fan_div[n - 1]);
  296. data->fan_min[n - 1] = r;
  297. if (n == 1)
  298. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff00) | (r << 8));
  299. else
  300. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xff) | r);
  301. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  302. if (data->fan_min[n - 1] == 0)
  303. data->alarm_mask &= (n == 1) ? ~0x20 : ~0x40;
  304. else
  305. data->alarm_mask |= (n == 1) ? 0x20 : 0x40;
  306. data->beep_mask &= data->alarm_mask;
  307. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  308. up(&data->update_lock);
  309. return count;
  310. }
  311. static ssize_t set_fan_div(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  312. {
  313. unsigned long v = simple_strtoul(buf, NULL, 10);
  314. u8 r;
  315. switch (v) {
  316. case 1: r = 0; break;
  317. case 2: r = 1; break;
  318. case 4: r = 2; break;
  319. case 8: r = 3; break;
  320. default:
  321. dev_err(&client->dev, "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n", v);
  322. return -EINVAL;
  323. }
  324. down(&data->update_lock);
  325. data->fan_div[n - 1] = r;
  326. if (n == 1)
  327. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0xc0) | (r << 6));
  328. else
  329. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x30) | (r << 4));
  330. up(&data->update_lock);
  331. return count;
  332. }
  333. static ssize_t set_fan_off(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  334. {
  335. u8 r = simple_strtoul(buf, NULL, 10)?1:0;
  336. down(&data->update_lock);
  337. data->fan_off = r;
  338. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x0c) | (r << 2));
  339. up(&data->update_lock);
  340. return count;
  341. }
  342. #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
  343. #define TEMP_TO_REG(val) (SENSORS_LIMIT(((((val)<0?(val)-500:(val)+500) / 1000)+130),0,255))
  344. static ssize_t get_temp_input(struct gl520_data *data, char *buf, int n)
  345. {
  346. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_input[n - 1]));
  347. }
  348. static ssize_t get_temp_max(struct gl520_data *data, char *buf, int n)
  349. {
  350. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[n - 1]));
  351. }
  352. static ssize_t get_temp_max_hyst(struct gl520_data *data, char *buf, int n)
  353. {
  354. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max_hyst[n - 1]));
  355. }
  356. static ssize_t set_temp_max(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  357. {
  358. long v = simple_strtol(buf, NULL, 10);
  359. down(&data->update_lock);
  360. data->temp_max[n - 1] = TEMP_TO_REG(v);;
  361. gl520_write_value(client, reg, data->temp_max[n - 1]);
  362. up(&data->update_lock);
  363. return count;
  364. }
  365. static ssize_t set_temp_max_hyst(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  366. {
  367. long v = simple_strtol(buf, NULL, 10);
  368. down(&data->update_lock);
  369. data->temp_max_hyst[n - 1] = TEMP_TO_REG(v);
  370. gl520_write_value(client, reg, data->temp_max_hyst[n - 1]);
  371. up(&data->update_lock);
  372. return count;
  373. }
  374. static ssize_t get_alarms(struct gl520_data *data, char *buf, int n)
  375. {
  376. return sprintf(buf, "%d\n", data->alarms);
  377. }
  378. static ssize_t get_beep_enable(struct gl520_data *data, char *buf, int n)
  379. {
  380. return sprintf(buf, "%d\n", data->beep_enable);
  381. }
  382. static ssize_t get_beep_mask(struct gl520_data *data, char *buf, int n)
  383. {
  384. return sprintf(buf, "%d\n", data->beep_mask);
  385. }
  386. static ssize_t set_beep_enable(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  387. {
  388. u8 r = simple_strtoul(buf, NULL, 10)?0:1;
  389. down(&data->update_lock);
  390. data->beep_enable = !r;
  391. gl520_write_value(client, reg, (gl520_read_value(client, reg) & ~0x04) | (r << 2));
  392. up(&data->update_lock);
  393. return count;
  394. }
  395. static ssize_t set_beep_mask(struct i2c_client *client, struct gl520_data *data, const char *buf, size_t count, int n, int reg)
  396. {
  397. u8 r = simple_strtoul(buf, NULL, 10);
  398. down(&data->update_lock);
  399. r &= data->alarm_mask;
  400. data->beep_mask = r;
  401. gl520_write_value(client, reg, r);
  402. up(&data->update_lock);
  403. return count;
  404. }
  405. /*
  406. * Real code
  407. */
  408. static int gl520_attach_adapter(struct i2c_adapter *adapter)
  409. {
  410. if (!(adapter->class & I2C_CLASS_HWMON))
  411. return 0;
  412. return i2c_detect(adapter, &addr_data, gl520_detect);
  413. }
  414. static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
  415. {
  416. struct i2c_client *new_client;
  417. struct gl520_data *data;
  418. int err = 0;
  419. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  420. I2C_FUNC_SMBUS_WORD_DATA))
  421. goto exit;
  422. /* OK. For now, we presume we have a valid client. We now create the
  423. client structure, even though we cannot fill it completely yet.
  424. But it allows us to access gl520_{read,write}_value. */
  425. if (!(data = kmalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
  426. err = -ENOMEM;
  427. goto exit;
  428. }
  429. memset(data, 0, sizeof(struct gl520_data));
  430. new_client = &data->client;
  431. i2c_set_clientdata(new_client, data);
  432. new_client->addr = address;
  433. new_client->adapter = adapter;
  434. new_client->driver = &gl520_driver;
  435. new_client->flags = 0;
  436. /* Determine the chip type. */
  437. if (kind < 0) {
  438. if ((gl520_read_value(new_client, GL520_REG_CHIP_ID) != 0x20) ||
  439. ((gl520_read_value(new_client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
  440. ((gl520_read_value(new_client, GL520_REG_CONF) & 0x80) != 0x00)) {
  441. dev_dbg(&new_client->dev, "Unknown chip type, skipping\n");
  442. goto exit_free;
  443. }
  444. }
  445. /* Fill in the remaining client fields */
  446. strlcpy(new_client->name, "gl520sm", I2C_NAME_SIZE);
  447. data->valid = 0;
  448. init_MUTEX(&data->update_lock);
  449. /* Tell the I2C layer a new client has arrived */
  450. if ((err = i2c_attach_client(new_client)))
  451. goto exit_free;
  452. /* Initialize the GL520SM chip */
  453. gl520_init_client(new_client);
  454. /* Register sysfs hooks */
  455. device_create_file_vid(new_client, 0);
  456. device_create_file_in(new_client, 0);
  457. device_create_file_in(new_client, 1);
  458. device_create_file_in(new_client, 2);
  459. device_create_file_in(new_client, 3);
  460. if (!data->two_temps)
  461. device_create_file_in(new_client, 4);
  462. device_create_file_fan(new_client, 1);
  463. device_create_file_fan(new_client, 2);
  464. device_create_file_fan_off(new_client, 1);
  465. device_create_file_temp(new_client, 1);
  466. if (data->two_temps)
  467. device_create_file_temp(new_client, 2);
  468. device_create_file_alarms(new_client);
  469. return 0;
  470. exit_free:
  471. kfree(data);
  472. exit:
  473. return err;
  474. }
  475. /* Called when we have found a new GL520SM. */
  476. static void gl520_init_client(struct i2c_client *client)
  477. {
  478. struct gl520_data *data = i2c_get_clientdata(client);
  479. u8 oldconf, conf;
  480. conf = oldconf = gl520_read_value(client, GL520_REG_CONF);
  481. data->alarm_mask = 0xff;
  482. data->vrm = i2c_which_vrm();
  483. if (extra_sensor_type == 1)
  484. conf &= ~0x10;
  485. else if (extra_sensor_type == 2)
  486. conf |= 0x10;
  487. data->two_temps = !(conf & 0x10);
  488. /* If IRQ# is disabled, we can safely force comparator mode */
  489. if (!(conf & 0x20))
  490. conf &= 0xf7;
  491. /* Enable monitoring if needed */
  492. conf |= 0x40;
  493. if (conf != oldconf)
  494. gl520_write_value(client, GL520_REG_CONF, conf);
  495. gl520_update_device(&(client->dev));
  496. if (data->fan_min[0] == 0)
  497. data->alarm_mask &= ~0x20;
  498. if (data->fan_min[1] == 0)
  499. data->alarm_mask &= ~0x40;
  500. data->beep_mask &= data->alarm_mask;
  501. gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
  502. }
  503. static int gl520_detach_client(struct i2c_client *client)
  504. {
  505. int err;
  506. if ((err = i2c_detach_client(client))) {
  507. dev_err(&client->dev, "Client deregistration failed, "
  508. "client not detached.\n");
  509. return err;
  510. }
  511. kfree(i2c_get_clientdata(client));
  512. return 0;
  513. }
  514. /* Registers 0x07 to 0x0c are word-sized, others are byte-sized
  515. GL520 uses a high-byte first convention */
  516. static int gl520_read_value(struct i2c_client *client, u8 reg)
  517. {
  518. if ((reg >= 0x07) && (reg <= 0x0c))
  519. return swab16(i2c_smbus_read_word_data(client, reg));
  520. else
  521. return i2c_smbus_read_byte_data(client, reg);
  522. }
  523. static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value)
  524. {
  525. if ((reg >= 0x07) && (reg <= 0x0c))
  526. return i2c_smbus_write_word_data(client, reg, swab16(value));
  527. else
  528. return i2c_smbus_write_byte_data(client, reg, value);
  529. }
  530. static struct gl520_data *gl520_update_device(struct device *dev)
  531. {
  532. struct i2c_client *client = to_i2c_client(dev);
  533. struct gl520_data *data = i2c_get_clientdata(client);
  534. int val;
  535. down(&data->update_lock);
  536. if ((jiffies - data->last_updated > 2 * HZ) ||
  537. (jiffies < data->last_updated) || !data->valid) {
  538. dev_dbg(&client->dev, "Starting gl520sm update\n");
  539. data->alarms = gl520_read_value(client, GL520_REG_ALARMS);
  540. data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
  541. data->vid = gl520_read_value(client, GL520_REG_VID_INPUT) & 0x1f;
  542. val = gl520_read_value(client, GL520_REG_IN0_LIMIT);
  543. data->in_min[0] = val & 0xff;
  544. data->in_max[0] = (val >> 8) & 0xff;
  545. val = gl520_read_value(client, GL520_REG_IN1_LIMIT);
  546. data->in_min[1] = val & 0xff;
  547. data->in_max[1] = (val >> 8) & 0xff;
  548. val = gl520_read_value(client, GL520_REG_IN2_LIMIT);
  549. data->in_min[2] = val & 0xff;
  550. data->in_max[2] = (val >> 8) & 0xff;
  551. val = gl520_read_value(client, GL520_REG_IN3_LIMIT);
  552. data->in_min[3] = val & 0xff;
  553. data->in_max[3] = (val >> 8) & 0xff;
  554. val = gl520_read_value(client, GL520_REG_FAN_INPUT);
  555. data->fan_input[0] = (val >> 8) & 0xff;
  556. data->fan_input[1] = val & 0xff;
  557. val = gl520_read_value(client, GL520_REG_FAN_MIN);
  558. data->fan_min[0] = (val >> 8) & 0xff;
  559. data->fan_min[1] = val & 0xff;
  560. data->temp_input[0] = gl520_read_value(client, GL520_REG_TEMP1_INPUT);
  561. data->temp_max[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX);
  562. data->temp_max_hyst[0] = gl520_read_value(client, GL520_REG_TEMP1_MAX_HYST);
  563. val = gl520_read_value(client, GL520_REG_FAN_DIV);
  564. data->fan_div[0] = (val >> 6) & 0x03;
  565. data->fan_div[1] = (val >> 4) & 0x03;
  566. data->fan_off = (val >> 2) & 0x01;
  567. data->alarms &= data->alarm_mask;
  568. val = gl520_read_value(client, GL520_REG_CONF);
  569. data->beep_enable = !((val >> 2) & 1);
  570. data->in_input[0] = gl520_read_value(client, GL520_REG_IN0_INPUT);
  571. data->in_input[1] = gl520_read_value(client, GL520_REG_IN1_INPUT);
  572. data->in_input[2] = gl520_read_value(client, GL520_REG_IN2_INPUT);
  573. data->in_input[3] = gl520_read_value(client, GL520_REG_IN3_INPUT);
  574. /* Temp1 and Vin4 are the same input */
  575. if (data->two_temps) {
  576. data->temp_input[1] = gl520_read_value(client, GL520_REG_TEMP2_INPUT);
  577. data->temp_max[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX);
  578. data->temp_max_hyst[1] = gl520_read_value(client, GL520_REG_TEMP2_MAX_HYST);
  579. } else {
  580. data->in_input[4] = gl520_read_value(client, GL520_REG_IN4_INPUT);
  581. data->in_min[4] = gl520_read_value(client, GL520_REG_IN4_MIN);
  582. data->in_max[4] = gl520_read_value(client, GL520_REG_IN4_MAX);
  583. }
  584. data->last_updated = jiffies;
  585. data->valid = 1;
  586. }
  587. up(&data->update_lock);
  588. return data;
  589. }
  590. static int __init sensors_gl520sm_init(void)
  591. {
  592. return i2c_add_driver(&gl520_driver);
  593. }
  594. static void __exit sensors_gl520sm_exit(void)
  595. {
  596. i2c_del_driver(&gl520_driver);
  597. }
  598. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  599. "Kyösti Mälkki <kmalkki@cc.hut.fi>, "
  600. "Maarten Deprez <maartendeprez@users.sourceforge.net>");
  601. MODULE_DESCRIPTION("GL520SM driver");
  602. MODULE_LICENSE("GPL");
  603. module_init(sensors_gl520sm_init);
  604. module_exit(sensors_gl520sm_exit);