asb100.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * asb100.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. *
  5. * Copyright (C) 2004 Mark M. Hoffman <mhoffman@lightlink.com>
  6. *
  7. * (derived from w83781d.c)
  8. *
  9. * Copyright (C) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
  10. * Philip Edelbrock <phil@netroedge.com>, and
  11. * Mark Studebaker <mdsxyz123@yahoo.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. /*
  28. * This driver supports the hardware sensor chips: Asus ASB100 and
  29. * ASB100-A "BACH".
  30. *
  31. * ASB100-A supports pwm1, while plain ASB100 does not. There is no known
  32. * way for the driver to tell which one is there.
  33. *
  34. * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  35. * asb100 7 3 1 4 0x31 0x0694 yes no
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/i2c.h>
  41. #include <linux/hwmon.h>
  42. #include <linux/hwmon-sysfs.h>
  43. #include <linux/hwmon-vid.h>
  44. #include <linux/err.h>
  45. #include <linux/init.h>
  46. #include <linux/jiffies.h>
  47. #include <linux/mutex.h>
  48. #include "lm75.h"
  49. /* I2C addresses to scan */
  50. static const unsigned short normal_i2c[] = { 0x2d, I2C_CLIENT_END };
  51. static unsigned short force_subclients[4];
  52. module_param_array(force_subclients, short, NULL, 0);
  53. MODULE_PARM_DESC(force_subclients,
  54. "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
  55. /* Voltage IN registers 0-6 */
  56. #define ASB100_REG_IN(nr) (0x20 + (nr))
  57. #define ASB100_REG_IN_MAX(nr) (0x2b + (nr * 2))
  58. #define ASB100_REG_IN_MIN(nr) (0x2c + (nr * 2))
  59. /* FAN IN registers 1-3 */
  60. #define ASB100_REG_FAN(nr) (0x28 + (nr))
  61. #define ASB100_REG_FAN_MIN(nr) (0x3b + (nr))
  62. /* TEMPERATURE registers 1-4 */
  63. static const u16 asb100_reg_temp[] = {0, 0x27, 0x150, 0x250, 0x17};
  64. static const u16 asb100_reg_temp_max[] = {0, 0x39, 0x155, 0x255, 0x18};
  65. static const u16 asb100_reg_temp_hyst[] = {0, 0x3a, 0x153, 0x253, 0x19};
  66. #define ASB100_REG_TEMP(nr) (asb100_reg_temp[nr])
  67. #define ASB100_REG_TEMP_MAX(nr) (asb100_reg_temp_max[nr])
  68. #define ASB100_REG_TEMP_HYST(nr) (asb100_reg_temp_hyst[nr])
  69. #define ASB100_REG_TEMP2_CONFIG 0x0152
  70. #define ASB100_REG_TEMP3_CONFIG 0x0252
  71. #define ASB100_REG_CONFIG 0x40
  72. #define ASB100_REG_ALARM1 0x41
  73. #define ASB100_REG_ALARM2 0x42
  74. #define ASB100_REG_SMIM1 0x43
  75. #define ASB100_REG_SMIM2 0x44
  76. #define ASB100_REG_VID_FANDIV 0x47
  77. #define ASB100_REG_I2C_ADDR 0x48
  78. #define ASB100_REG_CHIPID 0x49
  79. #define ASB100_REG_I2C_SUBADDR 0x4a
  80. #define ASB100_REG_PIN 0x4b
  81. #define ASB100_REG_IRQ 0x4c
  82. #define ASB100_REG_BANK 0x4e
  83. #define ASB100_REG_CHIPMAN 0x4f
  84. #define ASB100_REG_WCHIPID 0x58
  85. /* bit 7 -> enable, bits 0-3 -> duty cycle */
  86. #define ASB100_REG_PWM1 0x59
  87. /*
  88. * CONVERSIONS
  89. * Rounding and limit checking is only done on the TO_REG variants.
  90. */
  91. /* These constants are a guess, consistent w/ w83781d */
  92. #define ASB100_IN_MIN 0
  93. #define ASB100_IN_MAX 4080
  94. /*
  95. * IN: 1/1000 V (0V to 4.08V)
  96. * REG: 16mV/bit
  97. */
  98. static u8 IN_TO_REG(unsigned val)
  99. {
  100. unsigned nval = clamp_val(val, ASB100_IN_MIN, ASB100_IN_MAX);
  101. return (nval + 8) / 16;
  102. }
  103. static unsigned IN_FROM_REG(u8 reg)
  104. {
  105. return reg * 16;
  106. }
  107. static u8 FAN_TO_REG(long rpm, int div)
  108. {
  109. if (rpm == -1)
  110. return 0;
  111. if (rpm == 0)
  112. return 255;
  113. rpm = clamp_val(rpm, 1, 1000000);
  114. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  115. }
  116. static int FAN_FROM_REG(u8 val, int div)
  117. {
  118. return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
  119. }
  120. /* These constants are a guess, consistent w/ w83781d */
  121. #define ASB100_TEMP_MIN -128000
  122. #define ASB100_TEMP_MAX 127000
  123. /*
  124. * TEMP: 0.001C/bit (-128C to +127C)
  125. * REG: 1C/bit, two's complement
  126. */
  127. static u8 TEMP_TO_REG(long temp)
  128. {
  129. int ntemp = clamp_val(temp, ASB100_TEMP_MIN, ASB100_TEMP_MAX);
  130. ntemp += (ntemp < 0 ? -500 : 500);
  131. return (u8)(ntemp / 1000);
  132. }
  133. static int TEMP_FROM_REG(u8 reg)
  134. {
  135. return (s8)reg * 1000;
  136. }
  137. /*
  138. * PWM: 0 - 255 per sensors documentation
  139. * REG: (6.25% duty cycle per bit)
  140. */
  141. static u8 ASB100_PWM_TO_REG(int pwm)
  142. {
  143. pwm = clamp_val(pwm, 0, 255);
  144. return (u8)(pwm / 16);
  145. }
  146. static int ASB100_PWM_FROM_REG(u8 reg)
  147. {
  148. return reg * 16;
  149. }
  150. #define DIV_FROM_REG(val) (1 << (val))
  151. /*
  152. * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  153. * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
  154. */
  155. static u8 DIV_TO_REG(long val)
  156. {
  157. return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
  158. }
  159. /*
  160. * For each registered client, we need to keep some data in memory. That
  161. * data is pointed to by client->data. The structure itself is
  162. * dynamically allocated, at the same time the client itself is allocated.
  163. */
  164. struct asb100_data {
  165. struct device *hwmon_dev;
  166. struct mutex lock;
  167. struct mutex update_lock;
  168. unsigned long last_updated; /* In jiffies */
  169. /* array of 2 pointers to subclients */
  170. struct i2c_client *lm75[2];
  171. char valid; /* !=0 if following fields are valid */
  172. u8 in[7]; /* Register value */
  173. u8 in_max[7]; /* Register value */
  174. u8 in_min[7]; /* Register value */
  175. u8 fan[3]; /* Register value */
  176. u8 fan_min[3]; /* Register value */
  177. u16 temp[4]; /* Register value (0 and 3 are u8 only) */
  178. u16 temp_max[4]; /* Register value (0 and 3 are u8 only) */
  179. u16 temp_hyst[4]; /* Register value (0 and 3 are u8 only) */
  180. u8 fan_div[3]; /* Register encoding, right justified */
  181. u8 pwm; /* Register encoding */
  182. u8 vid; /* Register encoding, combined */
  183. u32 alarms; /* Register encoding, combined */
  184. u8 vrm;
  185. };
  186. static int asb100_read_value(struct i2c_client *client, u16 reg);
  187. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 val);
  188. static int asb100_probe(struct i2c_client *client,
  189. const struct i2c_device_id *id);
  190. static int asb100_detect(struct i2c_client *client,
  191. struct i2c_board_info *info);
  192. static int asb100_remove(struct i2c_client *client);
  193. static struct asb100_data *asb100_update_device(struct device *dev);
  194. static void asb100_init_client(struct i2c_client *client);
  195. static const struct i2c_device_id asb100_id[] = {
  196. { "asb100", 0 },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(i2c, asb100_id);
  200. static struct i2c_driver asb100_driver = {
  201. .class = I2C_CLASS_HWMON,
  202. .driver = {
  203. .name = "asb100",
  204. },
  205. .probe = asb100_probe,
  206. .remove = asb100_remove,
  207. .id_table = asb100_id,
  208. .detect = asb100_detect,
  209. .address_list = normal_i2c,
  210. };
  211. /* 7 Voltages */
  212. #define show_in_reg(reg) \
  213. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  214. char *buf) \
  215. { \
  216. int nr = to_sensor_dev_attr(attr)->index; \
  217. struct asb100_data *data = asb100_update_device(dev); \
  218. return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
  219. }
  220. show_in_reg(in)
  221. show_in_reg(in_min)
  222. show_in_reg(in_max)
  223. #define set_in_reg(REG, reg) \
  224. static ssize_t set_in_##reg(struct device *dev, struct device_attribute *attr, \
  225. const char *buf, size_t count) \
  226. { \
  227. int nr = to_sensor_dev_attr(attr)->index; \
  228. struct i2c_client *client = to_i2c_client(dev); \
  229. struct asb100_data *data = i2c_get_clientdata(client); \
  230. unsigned long val; \
  231. int err = kstrtoul(buf, 10, &val); \
  232. if (err) \
  233. return err; \
  234. mutex_lock(&data->update_lock); \
  235. data->in_##reg[nr] = IN_TO_REG(val); \
  236. asb100_write_value(client, ASB100_REG_IN_##REG(nr), \
  237. data->in_##reg[nr]); \
  238. mutex_unlock(&data->update_lock); \
  239. return count; \
  240. }
  241. set_in_reg(MIN, min)
  242. set_in_reg(MAX, max)
  243. #define sysfs_in(offset) \
  244. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  245. show_in, NULL, offset); \
  246. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  247. show_in_min, set_in_min, offset); \
  248. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  249. show_in_max, set_in_max, offset)
  250. sysfs_in(0);
  251. sysfs_in(1);
  252. sysfs_in(2);
  253. sysfs_in(3);
  254. sysfs_in(4);
  255. sysfs_in(5);
  256. sysfs_in(6);
  257. /* 3 Fans */
  258. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  259. char *buf)
  260. {
  261. int nr = to_sensor_dev_attr(attr)->index;
  262. struct asb100_data *data = asb100_update_device(dev);
  263. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  264. DIV_FROM_REG(data->fan_div[nr])));
  265. }
  266. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  267. char *buf)
  268. {
  269. int nr = to_sensor_dev_attr(attr)->index;
  270. struct asb100_data *data = asb100_update_device(dev);
  271. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  272. DIV_FROM_REG(data->fan_div[nr])));
  273. }
  274. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  275. char *buf)
  276. {
  277. int nr = to_sensor_dev_attr(attr)->index;
  278. struct asb100_data *data = asb100_update_device(dev);
  279. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  280. }
  281. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  282. const char *buf, size_t count)
  283. {
  284. int nr = to_sensor_dev_attr(attr)->index;
  285. struct i2c_client *client = to_i2c_client(dev);
  286. struct asb100_data *data = i2c_get_clientdata(client);
  287. unsigned long val;
  288. int err;
  289. err = kstrtoul(buf, 10, &val);
  290. if (err)
  291. return err;
  292. mutex_lock(&data->update_lock);
  293. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  294. asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
  295. mutex_unlock(&data->update_lock);
  296. return count;
  297. }
  298. /*
  299. * Note: we save and restore the fan minimum here, because its value is
  300. * determined in part by the fan divisor. This follows the principle of
  301. * least surprise; the user doesn't expect the fan minimum to change just
  302. * because the divisor changed.
  303. */
  304. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  305. const char *buf, size_t count)
  306. {
  307. int nr = to_sensor_dev_attr(attr)->index;
  308. struct i2c_client *client = to_i2c_client(dev);
  309. struct asb100_data *data = i2c_get_clientdata(client);
  310. unsigned long min;
  311. int reg;
  312. unsigned long val;
  313. int err;
  314. err = kstrtoul(buf, 10, &val);
  315. if (err)
  316. return err;
  317. mutex_lock(&data->update_lock);
  318. min = FAN_FROM_REG(data->fan_min[nr],
  319. DIV_FROM_REG(data->fan_div[nr]));
  320. data->fan_div[nr] = DIV_TO_REG(val);
  321. switch (nr) {
  322. case 0: /* fan 1 */
  323. reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  324. reg = (reg & 0xcf) | (data->fan_div[0] << 4);
  325. asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
  326. break;
  327. case 1: /* fan 2 */
  328. reg = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  329. reg = (reg & 0x3f) | (data->fan_div[1] << 6);
  330. asb100_write_value(client, ASB100_REG_VID_FANDIV, reg);
  331. break;
  332. case 2: /* fan 3 */
  333. reg = asb100_read_value(client, ASB100_REG_PIN);
  334. reg = (reg & 0x3f) | (data->fan_div[2] << 6);
  335. asb100_write_value(client, ASB100_REG_PIN, reg);
  336. break;
  337. }
  338. data->fan_min[nr] =
  339. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  340. asb100_write_value(client, ASB100_REG_FAN_MIN(nr), data->fan_min[nr]);
  341. mutex_unlock(&data->update_lock);
  342. return count;
  343. }
  344. #define sysfs_fan(offset) \
  345. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  346. show_fan, NULL, offset - 1); \
  347. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  348. show_fan_min, set_fan_min, offset - 1); \
  349. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  350. show_fan_div, set_fan_div, offset - 1)
  351. sysfs_fan(1);
  352. sysfs_fan(2);
  353. sysfs_fan(3);
  354. /* 4 Temp. Sensors */
  355. static int sprintf_temp_from_reg(u16 reg, char *buf, int nr)
  356. {
  357. int ret = 0;
  358. switch (nr) {
  359. case 1: case 2:
  360. ret = sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(reg));
  361. break;
  362. case 0: case 3: default:
  363. ret = sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
  364. break;
  365. }
  366. return ret;
  367. }
  368. #define show_temp_reg(reg) \
  369. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  370. char *buf) \
  371. { \
  372. int nr = to_sensor_dev_attr(attr)->index; \
  373. struct asb100_data *data = asb100_update_device(dev); \
  374. return sprintf_temp_from_reg(data->reg[nr], buf, nr); \
  375. }
  376. show_temp_reg(temp);
  377. show_temp_reg(temp_max);
  378. show_temp_reg(temp_hyst);
  379. #define set_temp_reg(REG, reg) \
  380. static ssize_t set_##reg(struct device *dev, struct device_attribute *attr, \
  381. const char *buf, size_t count) \
  382. { \
  383. int nr = to_sensor_dev_attr(attr)->index; \
  384. struct i2c_client *client = to_i2c_client(dev); \
  385. struct asb100_data *data = i2c_get_clientdata(client); \
  386. long val; \
  387. int err = kstrtol(buf, 10, &val); \
  388. if (err) \
  389. return err; \
  390. mutex_lock(&data->update_lock); \
  391. switch (nr) { \
  392. case 1: case 2: \
  393. data->reg[nr] = LM75_TEMP_TO_REG(val); \
  394. break; \
  395. case 0: case 3: default: \
  396. data->reg[nr] = TEMP_TO_REG(val); \
  397. break; \
  398. } \
  399. asb100_write_value(client, ASB100_REG_TEMP_##REG(nr+1), \
  400. data->reg[nr]); \
  401. mutex_unlock(&data->update_lock); \
  402. return count; \
  403. }
  404. set_temp_reg(MAX, temp_max);
  405. set_temp_reg(HYST, temp_hyst);
  406. #define sysfs_temp(num) \
  407. static SENSOR_DEVICE_ATTR(temp##num##_input, S_IRUGO, \
  408. show_temp, NULL, num - 1); \
  409. static SENSOR_DEVICE_ATTR(temp##num##_max, S_IRUGO | S_IWUSR, \
  410. show_temp_max, set_temp_max, num - 1); \
  411. static SENSOR_DEVICE_ATTR(temp##num##_max_hyst, S_IRUGO | S_IWUSR, \
  412. show_temp_hyst, set_temp_hyst, num - 1)
  413. sysfs_temp(1);
  414. sysfs_temp(2);
  415. sysfs_temp(3);
  416. sysfs_temp(4);
  417. /* VID */
  418. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  419. char *buf)
  420. {
  421. struct asb100_data *data = asb100_update_device(dev);
  422. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  423. }
  424. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  425. /* VRM */
  426. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  427. char *buf)
  428. {
  429. struct asb100_data *data = dev_get_drvdata(dev);
  430. return sprintf(buf, "%d\n", data->vrm);
  431. }
  432. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. struct asb100_data *data = dev_get_drvdata(dev);
  436. unsigned long val;
  437. int err;
  438. err = kstrtoul(buf, 10, &val);
  439. if (err)
  440. return err;
  441. if (val > 255)
  442. return -EINVAL;
  443. data->vrm = val;
  444. return count;
  445. }
  446. /* Alarms */
  447. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  448. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  449. char *buf)
  450. {
  451. struct asb100_data *data = asb100_update_device(dev);
  452. return sprintf(buf, "%u\n", data->alarms);
  453. }
  454. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  455. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  456. char *buf)
  457. {
  458. int bitnr = to_sensor_dev_attr(attr)->index;
  459. struct asb100_data *data = asb100_update_device(dev);
  460. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  461. }
  462. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  463. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  464. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  465. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  466. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  467. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  468. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  469. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11);
  470. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  471. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  472. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
  473. /* 1 PWM */
  474. static ssize_t show_pwm1(struct device *dev, struct device_attribute *attr,
  475. char *buf)
  476. {
  477. struct asb100_data *data = asb100_update_device(dev);
  478. return sprintf(buf, "%d\n", ASB100_PWM_FROM_REG(data->pwm & 0x0f));
  479. }
  480. static ssize_t set_pwm1(struct device *dev, struct device_attribute *attr,
  481. const char *buf, size_t count)
  482. {
  483. struct i2c_client *client = to_i2c_client(dev);
  484. struct asb100_data *data = i2c_get_clientdata(client);
  485. unsigned long val;
  486. int err;
  487. err = kstrtoul(buf, 10, &val);
  488. if (err)
  489. return err;
  490. mutex_lock(&data->update_lock);
  491. data->pwm &= 0x80; /* keep the enable bit */
  492. data->pwm |= (0x0f & ASB100_PWM_TO_REG(val));
  493. asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
  494. mutex_unlock(&data->update_lock);
  495. return count;
  496. }
  497. static ssize_t show_pwm_enable1(struct device *dev,
  498. struct device_attribute *attr, char *buf)
  499. {
  500. struct asb100_data *data = asb100_update_device(dev);
  501. return sprintf(buf, "%d\n", (data->pwm & 0x80) ? 1 : 0);
  502. }
  503. static ssize_t set_pwm_enable1(struct device *dev,
  504. struct device_attribute *attr, const char *buf, size_t count)
  505. {
  506. struct i2c_client *client = to_i2c_client(dev);
  507. struct asb100_data *data = i2c_get_clientdata(client);
  508. unsigned long val;
  509. int err;
  510. err = kstrtoul(buf, 10, &val);
  511. if (err)
  512. return err;
  513. mutex_lock(&data->update_lock);
  514. data->pwm &= 0x0f; /* keep the duty cycle bits */
  515. data->pwm |= (val ? 0x80 : 0x00);
  516. asb100_write_value(client, ASB100_REG_PWM1, data->pwm);
  517. mutex_unlock(&data->update_lock);
  518. return count;
  519. }
  520. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm1, set_pwm1);
  521. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  522. show_pwm_enable1, set_pwm_enable1);
  523. static struct attribute *asb100_attributes[] = {
  524. &sensor_dev_attr_in0_input.dev_attr.attr,
  525. &sensor_dev_attr_in0_min.dev_attr.attr,
  526. &sensor_dev_attr_in0_max.dev_attr.attr,
  527. &sensor_dev_attr_in1_input.dev_attr.attr,
  528. &sensor_dev_attr_in1_min.dev_attr.attr,
  529. &sensor_dev_attr_in1_max.dev_attr.attr,
  530. &sensor_dev_attr_in2_input.dev_attr.attr,
  531. &sensor_dev_attr_in2_min.dev_attr.attr,
  532. &sensor_dev_attr_in2_max.dev_attr.attr,
  533. &sensor_dev_attr_in3_input.dev_attr.attr,
  534. &sensor_dev_attr_in3_min.dev_attr.attr,
  535. &sensor_dev_attr_in3_max.dev_attr.attr,
  536. &sensor_dev_attr_in4_input.dev_attr.attr,
  537. &sensor_dev_attr_in4_min.dev_attr.attr,
  538. &sensor_dev_attr_in4_max.dev_attr.attr,
  539. &sensor_dev_attr_in5_input.dev_attr.attr,
  540. &sensor_dev_attr_in5_min.dev_attr.attr,
  541. &sensor_dev_attr_in5_max.dev_attr.attr,
  542. &sensor_dev_attr_in6_input.dev_attr.attr,
  543. &sensor_dev_attr_in6_min.dev_attr.attr,
  544. &sensor_dev_attr_in6_max.dev_attr.attr,
  545. &sensor_dev_attr_fan1_input.dev_attr.attr,
  546. &sensor_dev_attr_fan1_min.dev_attr.attr,
  547. &sensor_dev_attr_fan1_div.dev_attr.attr,
  548. &sensor_dev_attr_fan2_input.dev_attr.attr,
  549. &sensor_dev_attr_fan2_min.dev_attr.attr,
  550. &sensor_dev_attr_fan2_div.dev_attr.attr,
  551. &sensor_dev_attr_fan3_input.dev_attr.attr,
  552. &sensor_dev_attr_fan3_min.dev_attr.attr,
  553. &sensor_dev_attr_fan3_div.dev_attr.attr,
  554. &sensor_dev_attr_temp1_input.dev_attr.attr,
  555. &sensor_dev_attr_temp1_max.dev_attr.attr,
  556. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  557. &sensor_dev_attr_temp2_input.dev_attr.attr,
  558. &sensor_dev_attr_temp2_max.dev_attr.attr,
  559. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  560. &sensor_dev_attr_temp3_input.dev_attr.attr,
  561. &sensor_dev_attr_temp3_max.dev_attr.attr,
  562. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  563. &sensor_dev_attr_temp4_input.dev_attr.attr,
  564. &sensor_dev_attr_temp4_max.dev_attr.attr,
  565. &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
  566. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  567. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  568. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  569. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  570. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  571. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  572. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  573. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  574. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  575. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  576. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  577. &dev_attr_cpu0_vid.attr,
  578. &dev_attr_vrm.attr,
  579. &dev_attr_alarms.attr,
  580. &dev_attr_pwm1.attr,
  581. &dev_attr_pwm1_enable.attr,
  582. NULL
  583. };
  584. static const struct attribute_group asb100_group = {
  585. .attrs = asb100_attributes,
  586. };
  587. static int asb100_detect_subclients(struct i2c_client *client)
  588. {
  589. int i, id, err;
  590. int address = client->addr;
  591. unsigned short sc_addr[2];
  592. struct asb100_data *data = i2c_get_clientdata(client);
  593. struct i2c_adapter *adapter = client->adapter;
  594. id = i2c_adapter_id(adapter);
  595. if (force_subclients[0] == id && force_subclients[1] == address) {
  596. for (i = 2; i <= 3; i++) {
  597. if (force_subclients[i] < 0x48 ||
  598. force_subclients[i] > 0x4f) {
  599. dev_err(&client->dev,
  600. "invalid subclient address %d; must be 0x48-0x4f\n",
  601. force_subclients[i]);
  602. err = -ENODEV;
  603. goto ERROR_SC_2;
  604. }
  605. }
  606. asb100_write_value(client, ASB100_REG_I2C_SUBADDR,
  607. (force_subclients[2] & 0x07) |
  608. ((force_subclients[3] & 0x07) << 4));
  609. sc_addr[0] = force_subclients[2];
  610. sc_addr[1] = force_subclients[3];
  611. } else {
  612. int val = asb100_read_value(client, ASB100_REG_I2C_SUBADDR);
  613. sc_addr[0] = 0x48 + (val & 0x07);
  614. sc_addr[1] = 0x48 + ((val >> 4) & 0x07);
  615. }
  616. if (sc_addr[0] == sc_addr[1]) {
  617. dev_err(&client->dev,
  618. "duplicate addresses 0x%x for subclients\n",
  619. sc_addr[0]);
  620. err = -ENODEV;
  621. goto ERROR_SC_2;
  622. }
  623. data->lm75[0] = i2c_new_dummy(adapter, sc_addr[0]);
  624. if (!data->lm75[0]) {
  625. dev_err(&client->dev,
  626. "subclient %d registration at address 0x%x failed.\n",
  627. 1, sc_addr[0]);
  628. err = -ENOMEM;
  629. goto ERROR_SC_2;
  630. }
  631. data->lm75[1] = i2c_new_dummy(adapter, sc_addr[1]);
  632. if (!data->lm75[1]) {
  633. dev_err(&client->dev,
  634. "subclient %d registration at address 0x%x failed.\n",
  635. 2, sc_addr[1]);
  636. err = -ENOMEM;
  637. goto ERROR_SC_3;
  638. }
  639. return 0;
  640. /* Undo inits in case of errors */
  641. ERROR_SC_3:
  642. i2c_unregister_device(data->lm75[0]);
  643. ERROR_SC_2:
  644. return err;
  645. }
  646. /* Return 0 if detection is successful, -ENODEV otherwise */
  647. static int asb100_detect(struct i2c_client *client,
  648. struct i2c_board_info *info)
  649. {
  650. struct i2c_adapter *adapter = client->adapter;
  651. int val1, val2;
  652. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  653. pr_debug("detect failed, smbus byte data not supported!\n");
  654. return -ENODEV;
  655. }
  656. val1 = i2c_smbus_read_byte_data(client, ASB100_REG_BANK);
  657. val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
  658. /* If we're in bank 0 */
  659. if ((!(val1 & 0x07)) &&
  660. /* Check for ASB100 ID (low byte) */
  661. (((!(val1 & 0x80)) && (val2 != 0x94)) ||
  662. /* Check for ASB100 ID (high byte ) */
  663. ((val1 & 0x80) && (val2 != 0x06)))) {
  664. pr_debug("detect failed, bad chip id 0x%02x!\n", val2);
  665. return -ENODEV;
  666. }
  667. /* Put it now into bank 0 and Vendor ID High Byte */
  668. i2c_smbus_write_byte_data(client, ASB100_REG_BANK,
  669. (i2c_smbus_read_byte_data(client, ASB100_REG_BANK) & 0x78)
  670. | 0x80);
  671. /* Determine the chip type. */
  672. val1 = i2c_smbus_read_byte_data(client, ASB100_REG_WCHIPID);
  673. val2 = i2c_smbus_read_byte_data(client, ASB100_REG_CHIPMAN);
  674. if (val1 != 0x31 || val2 != 0x06)
  675. return -ENODEV;
  676. strlcpy(info->type, "asb100", I2C_NAME_SIZE);
  677. return 0;
  678. }
  679. static int asb100_probe(struct i2c_client *client,
  680. const struct i2c_device_id *id)
  681. {
  682. int err;
  683. struct asb100_data *data;
  684. data = devm_kzalloc(&client->dev, sizeof(struct asb100_data),
  685. GFP_KERNEL);
  686. if (!data)
  687. return -ENOMEM;
  688. i2c_set_clientdata(client, data);
  689. mutex_init(&data->lock);
  690. mutex_init(&data->update_lock);
  691. /* Attach secondary lm75 clients */
  692. err = asb100_detect_subclients(client);
  693. if (err)
  694. return err;
  695. /* Initialize the chip */
  696. asb100_init_client(client);
  697. /* A few vars need to be filled upon startup */
  698. data->fan_min[0] = asb100_read_value(client, ASB100_REG_FAN_MIN(0));
  699. data->fan_min[1] = asb100_read_value(client, ASB100_REG_FAN_MIN(1));
  700. data->fan_min[2] = asb100_read_value(client, ASB100_REG_FAN_MIN(2));
  701. /* Register sysfs hooks */
  702. err = sysfs_create_group(&client->dev.kobj, &asb100_group);
  703. if (err)
  704. goto ERROR3;
  705. data->hwmon_dev = hwmon_device_register(&client->dev);
  706. if (IS_ERR(data->hwmon_dev)) {
  707. err = PTR_ERR(data->hwmon_dev);
  708. goto ERROR4;
  709. }
  710. return 0;
  711. ERROR4:
  712. sysfs_remove_group(&client->dev.kobj, &asb100_group);
  713. ERROR3:
  714. i2c_unregister_device(data->lm75[1]);
  715. i2c_unregister_device(data->lm75[0]);
  716. return err;
  717. }
  718. static int asb100_remove(struct i2c_client *client)
  719. {
  720. struct asb100_data *data = i2c_get_clientdata(client);
  721. hwmon_device_unregister(data->hwmon_dev);
  722. sysfs_remove_group(&client->dev.kobj, &asb100_group);
  723. i2c_unregister_device(data->lm75[1]);
  724. i2c_unregister_device(data->lm75[0]);
  725. return 0;
  726. }
  727. /*
  728. * The SMBus locks itself, usually, but nothing may access the chip between
  729. * bank switches.
  730. */
  731. static int asb100_read_value(struct i2c_client *client, u16 reg)
  732. {
  733. struct asb100_data *data = i2c_get_clientdata(client);
  734. struct i2c_client *cl;
  735. int res, bank;
  736. mutex_lock(&data->lock);
  737. bank = (reg >> 8) & 0x0f;
  738. if (bank > 2)
  739. /* switch banks */
  740. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  741. if (bank == 0 || bank > 2) {
  742. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  743. } else {
  744. /* switch to subclient */
  745. cl = data->lm75[bank - 1];
  746. /* convert from ISA to LM75 I2C addresses */
  747. switch (reg & 0xff) {
  748. case 0x50: /* TEMP */
  749. res = i2c_smbus_read_word_swapped(cl, 0);
  750. break;
  751. case 0x52: /* CONFIG */
  752. res = i2c_smbus_read_byte_data(cl, 1);
  753. break;
  754. case 0x53: /* HYST */
  755. res = i2c_smbus_read_word_swapped(cl, 2);
  756. break;
  757. case 0x55: /* MAX */
  758. default:
  759. res = i2c_smbus_read_word_swapped(cl, 3);
  760. break;
  761. }
  762. }
  763. if (bank > 2)
  764. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  765. mutex_unlock(&data->lock);
  766. return res;
  767. }
  768. static void asb100_write_value(struct i2c_client *client, u16 reg, u16 value)
  769. {
  770. struct asb100_data *data = i2c_get_clientdata(client);
  771. struct i2c_client *cl;
  772. int bank;
  773. mutex_lock(&data->lock);
  774. bank = (reg >> 8) & 0x0f;
  775. if (bank > 2)
  776. /* switch banks */
  777. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, bank);
  778. if (bank == 0 || bank > 2) {
  779. i2c_smbus_write_byte_data(client, reg & 0xff, value & 0xff);
  780. } else {
  781. /* switch to subclient */
  782. cl = data->lm75[bank - 1];
  783. /* convert from ISA to LM75 I2C addresses */
  784. switch (reg & 0xff) {
  785. case 0x52: /* CONFIG */
  786. i2c_smbus_write_byte_data(cl, 1, value & 0xff);
  787. break;
  788. case 0x53: /* HYST */
  789. i2c_smbus_write_word_swapped(cl, 2, value);
  790. break;
  791. case 0x55: /* MAX */
  792. i2c_smbus_write_word_swapped(cl, 3, value);
  793. break;
  794. }
  795. }
  796. if (bank > 2)
  797. i2c_smbus_write_byte_data(client, ASB100_REG_BANK, 0);
  798. mutex_unlock(&data->lock);
  799. }
  800. static void asb100_init_client(struct i2c_client *client)
  801. {
  802. struct asb100_data *data = i2c_get_clientdata(client);
  803. data->vrm = vid_which_vrm();
  804. /* Start monitoring */
  805. asb100_write_value(client, ASB100_REG_CONFIG,
  806. (asb100_read_value(client, ASB100_REG_CONFIG) & 0xf7) | 0x01);
  807. }
  808. static struct asb100_data *asb100_update_device(struct device *dev)
  809. {
  810. struct i2c_client *client = to_i2c_client(dev);
  811. struct asb100_data *data = i2c_get_clientdata(client);
  812. int i;
  813. mutex_lock(&data->update_lock);
  814. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  815. || !data->valid) {
  816. dev_dbg(&client->dev, "starting device update...\n");
  817. /* 7 voltage inputs */
  818. for (i = 0; i < 7; i++) {
  819. data->in[i] = asb100_read_value(client,
  820. ASB100_REG_IN(i));
  821. data->in_min[i] = asb100_read_value(client,
  822. ASB100_REG_IN_MIN(i));
  823. data->in_max[i] = asb100_read_value(client,
  824. ASB100_REG_IN_MAX(i));
  825. }
  826. /* 3 fan inputs */
  827. for (i = 0; i < 3; i++) {
  828. data->fan[i] = asb100_read_value(client,
  829. ASB100_REG_FAN(i));
  830. data->fan_min[i] = asb100_read_value(client,
  831. ASB100_REG_FAN_MIN(i));
  832. }
  833. /* 4 temperature inputs */
  834. for (i = 1; i <= 4; i++) {
  835. data->temp[i-1] = asb100_read_value(client,
  836. ASB100_REG_TEMP(i));
  837. data->temp_max[i-1] = asb100_read_value(client,
  838. ASB100_REG_TEMP_MAX(i));
  839. data->temp_hyst[i-1] = asb100_read_value(client,
  840. ASB100_REG_TEMP_HYST(i));
  841. }
  842. /* VID and fan divisors */
  843. i = asb100_read_value(client, ASB100_REG_VID_FANDIV);
  844. data->vid = i & 0x0f;
  845. data->vid |= (asb100_read_value(client,
  846. ASB100_REG_CHIPID) & 0x01) << 4;
  847. data->fan_div[0] = (i >> 4) & 0x03;
  848. data->fan_div[1] = (i >> 6) & 0x03;
  849. data->fan_div[2] = (asb100_read_value(client,
  850. ASB100_REG_PIN) >> 6) & 0x03;
  851. /* PWM */
  852. data->pwm = asb100_read_value(client, ASB100_REG_PWM1);
  853. /* alarms */
  854. data->alarms = asb100_read_value(client, ASB100_REG_ALARM1) +
  855. (asb100_read_value(client, ASB100_REG_ALARM2) << 8);
  856. data->last_updated = jiffies;
  857. data->valid = 1;
  858. dev_dbg(&client->dev, "... device update complete\n");
  859. }
  860. mutex_unlock(&data->update_lock);
  861. return data;
  862. }
  863. module_i2c_driver(asb100_driver);
  864. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  865. MODULE_DESCRIPTION("ASB100 Bach driver");
  866. MODULE_LICENSE("GPL");