emc2103.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. emc2103.c - Support for SMSC EMC2103
  3. Copyright (c) 2010 SMSC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/err.h>
  24. #include <linux/mutex.h>
  25. /* Addresses scanned */
  26. static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
  27. static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
  28. static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
  29. static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
  30. #define REG_CONF1 0x20
  31. #define REG_TEMP_MAX_ALARM 0x24
  32. #define REG_TEMP_MIN_ALARM 0x25
  33. #define REG_FAN_CONF1 0x42
  34. #define REG_FAN_TARGET_LO 0x4c
  35. #define REG_FAN_TARGET_HI 0x4d
  36. #define REG_FAN_TACH_HI 0x4e
  37. #define REG_FAN_TACH_LO 0x4f
  38. #define REG_PRODUCT_ID 0xfd
  39. #define REG_MFG_ID 0xfe
  40. /* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
  41. #define FAN_RPM_FACTOR 3932160
  42. /* 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
  43. * in anti-parallel mode, and in this configuration both can be read
  44. * independently (so we have 4 temperature inputs). The device can't
  45. * detect if it's connected in this mode, so we have to manually enable
  46. * it. Default is to leave the device in the state it's already in (-1).
  47. * This parameter allows APD mode to be optionally forced on or off */
  48. static int apd = -1;
  49. module_param(apd, bool, 0);
  50. MODULE_PARM_DESC(init, "Set to zero to disable anti-parallel diode mode");
  51. struct temperature {
  52. s8 degrees;
  53. u8 fraction; /* 0-7 multiples of 0.125 */
  54. };
  55. struct emc2103_data {
  56. struct device *hwmon_dev;
  57. struct mutex update_lock;
  58. bool valid; /* registers are valid */
  59. bool fan_rpm_control;
  60. int temp_count; /* num of temp sensors */
  61. unsigned long last_updated; /* in jiffies */
  62. struct temperature temp[4]; /* internal + 3 external */
  63. s8 temp_min[4]; /* no fractional part */
  64. s8 temp_max[4]; /* no fractional part */
  65. u8 temp_min_alarm;
  66. u8 temp_max_alarm;
  67. u8 fan_multiplier;
  68. u16 fan_tach;
  69. u16 fan_target;
  70. };
  71. static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
  72. {
  73. int status = i2c_smbus_read_byte_data(client, i2c_reg);
  74. if (status < 0) {
  75. dev_warn(&client->dev, "reg 0x%02x, err %d\n",
  76. i2c_reg, status);
  77. } else {
  78. *output = status;
  79. }
  80. return status;
  81. }
  82. static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
  83. struct temperature *temp)
  84. {
  85. u8 degrees, fractional;
  86. if (read_u8_from_i2c(client, i2c_reg, &degrees) < 0)
  87. return;
  88. if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
  89. return;
  90. temp->degrees = degrees;
  91. temp->fraction = (fractional & 0xe0) >> 5;
  92. }
  93. static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
  94. u8 hi_addr, u8 lo_addr)
  95. {
  96. u8 high_byte, lo_byte;
  97. if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
  98. return;
  99. if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
  100. return;
  101. *output = ((u16)high_byte << 5) | (lo_byte >> 3);
  102. }
  103. static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
  104. {
  105. u8 high_byte = (new_target & 0x1fe0) >> 5;
  106. u8 low_byte = (new_target & 0x001f) << 3;
  107. i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
  108. i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
  109. }
  110. static void read_fan_config_from_i2c(struct i2c_client *client)
  111. {
  112. struct emc2103_data *data = i2c_get_clientdata(client);
  113. u8 conf1;
  114. if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
  115. return;
  116. data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
  117. data->fan_rpm_control = (conf1 & 0x80) != 0;
  118. }
  119. static struct emc2103_data *emc2103_update_device(struct device *dev)
  120. {
  121. struct i2c_client *client = to_i2c_client(dev);
  122. struct emc2103_data *data = i2c_get_clientdata(client);
  123. mutex_lock(&data->update_lock);
  124. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  125. || !data->valid) {
  126. int i;
  127. for (i = 0; i < data->temp_count; i++) {
  128. read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
  129. read_u8_from_i2c(client, REG_TEMP_MIN[i],
  130. &data->temp_min[i]);
  131. read_u8_from_i2c(client, REG_TEMP_MAX[i],
  132. &data->temp_max[i]);
  133. }
  134. read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
  135. &data->temp_min_alarm);
  136. read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
  137. &data->temp_max_alarm);
  138. read_fan_from_i2c(client, &data->fan_tach,
  139. REG_FAN_TACH_HI, REG_FAN_TACH_LO);
  140. read_fan_from_i2c(client, &data->fan_target,
  141. REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
  142. read_fan_config_from_i2c(client);
  143. data->last_updated = jiffies;
  144. data->valid = true;
  145. }
  146. mutex_unlock(&data->update_lock);
  147. return data;
  148. }
  149. static ssize_t
  150. show_temp(struct device *dev, struct device_attribute *da, char *buf)
  151. {
  152. int nr = to_sensor_dev_attr(da)->index;
  153. struct emc2103_data *data = emc2103_update_device(dev);
  154. int millidegrees = data->temp[nr].degrees * 1000
  155. + data->temp[nr].fraction * 125;
  156. return sprintf(buf, "%d\n", millidegrees);
  157. }
  158. static ssize_t
  159. show_temp_min(struct device *dev, struct device_attribute *da, char *buf)
  160. {
  161. int nr = to_sensor_dev_attr(da)->index;
  162. struct emc2103_data *data = emc2103_update_device(dev);
  163. int millidegrees = data->temp_min[nr] * 1000;
  164. return sprintf(buf, "%d\n", millidegrees);
  165. }
  166. static ssize_t
  167. show_temp_max(struct device *dev, struct device_attribute *da, char *buf)
  168. {
  169. int nr = to_sensor_dev_attr(da)->index;
  170. struct emc2103_data *data = emc2103_update_device(dev);
  171. int millidegrees = data->temp_max[nr] * 1000;
  172. return sprintf(buf, "%d\n", millidegrees);
  173. }
  174. static ssize_t
  175. show_temp_fault(struct device *dev, struct device_attribute *da, char *buf)
  176. {
  177. int nr = to_sensor_dev_attr(da)->index;
  178. struct emc2103_data *data = emc2103_update_device(dev);
  179. bool fault = (data->temp[nr].degrees == -128);
  180. return sprintf(buf, "%d\n", fault ? 1 : 0);
  181. }
  182. static ssize_t
  183. show_temp_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
  184. {
  185. int nr = to_sensor_dev_attr(da)->index;
  186. struct emc2103_data *data = emc2103_update_device(dev);
  187. bool alarm = data->temp_min_alarm & (1 << nr);
  188. return sprintf(buf, "%d\n", alarm ? 1 : 0);
  189. }
  190. static ssize_t
  191. show_temp_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
  192. {
  193. int nr = to_sensor_dev_attr(da)->index;
  194. struct emc2103_data *data = emc2103_update_device(dev);
  195. bool alarm = data->temp_max_alarm & (1 << nr);
  196. return sprintf(buf, "%d\n", alarm ? 1 : 0);
  197. }
  198. static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
  199. const char *buf, size_t count)
  200. {
  201. int nr = to_sensor_dev_attr(da)->index;
  202. struct i2c_client *client = to_i2c_client(dev);
  203. struct emc2103_data *data = i2c_get_clientdata(client);
  204. long val;
  205. int result = strict_strtol(buf, 10, &val);
  206. if (result < 0)
  207. return -EINVAL;
  208. val = DIV_ROUND_CLOSEST(val, 1000);
  209. if ((val < -63) || (val > 127))
  210. return -EINVAL;
  211. mutex_lock(&data->update_lock);
  212. data->temp_min[nr] = val;
  213. i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
  214. mutex_unlock(&data->update_lock);
  215. return count;
  216. }
  217. static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
  218. const char *buf, size_t count)
  219. {
  220. int nr = to_sensor_dev_attr(da)->index;
  221. struct i2c_client *client = to_i2c_client(dev);
  222. struct emc2103_data *data = i2c_get_clientdata(client);
  223. long val;
  224. int result = strict_strtol(buf, 10, &val);
  225. if (result < 0)
  226. return -EINVAL;
  227. val = DIV_ROUND_CLOSEST(val, 1000);
  228. if ((val < -63) || (val > 127))
  229. return -EINVAL;
  230. mutex_lock(&data->update_lock);
  231. data->temp_max[nr] = val;
  232. i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
  233. mutex_unlock(&data->update_lock);
  234. return count;
  235. }
  236. static ssize_t
  237. show_fan(struct device *dev, struct device_attribute *da, char *buf)
  238. {
  239. struct emc2103_data *data = emc2103_update_device(dev);
  240. int rpm = 0;
  241. if (data->fan_tach != 0)
  242. rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
  243. return sprintf(buf, "%d\n", rpm);
  244. }
  245. static ssize_t
  246. show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
  247. {
  248. struct emc2103_data *data = emc2103_update_device(dev);
  249. int fan_div = 8 / data->fan_multiplier;
  250. return sprintf(buf, "%d\n", fan_div);
  251. }
  252. /* Note: we also update the fan target here, because its value is
  253. determined in part by the fan clock divider. This follows the principle
  254. of least surprise; the user doesn't expect the fan target to change just
  255. because the divider changed. */
  256. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  257. const char *buf, size_t count)
  258. {
  259. struct emc2103_data *data = emc2103_update_device(dev);
  260. struct i2c_client *client = to_i2c_client(dev);
  261. int new_range_bits, old_div = 8 / data->fan_multiplier;
  262. long new_div;
  263. int status = strict_strtol(buf, 10, &new_div);
  264. if (status < 0)
  265. return -EINVAL;
  266. if (new_div == old_div) /* No change */
  267. return count;
  268. switch (new_div) {
  269. case 1:
  270. new_range_bits = 3;
  271. break;
  272. case 2:
  273. new_range_bits = 2;
  274. break;
  275. case 4:
  276. new_range_bits = 1;
  277. break;
  278. case 8:
  279. new_range_bits = 0;
  280. break;
  281. default:
  282. return -EINVAL;
  283. }
  284. mutex_lock(&data->update_lock);
  285. status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
  286. if (status < 0) {
  287. dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
  288. REG_FAN_CONF1, status);
  289. mutex_unlock(&data->update_lock);
  290. return -EIO;
  291. }
  292. status &= 0x9F;
  293. status |= (new_range_bits << 5);
  294. i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
  295. data->fan_multiplier = 8 / new_div;
  296. /* update fan target if high byte is not disabled */
  297. if ((data->fan_target & 0x1fe0) != 0x1fe0) {
  298. u16 new_target = (data->fan_target * old_div) / new_div;
  299. data->fan_target = min(new_target, (u16)0x1fff);
  300. write_fan_target_to_i2c(client, data->fan_target);
  301. }
  302. /* invalidate data to force re-read from hardware */
  303. data->valid = false;
  304. mutex_unlock(&data->update_lock);
  305. return count;
  306. }
  307. static ssize_t
  308. show_fan_target(struct device *dev, struct device_attribute *da, char *buf)
  309. {
  310. struct emc2103_data *data = emc2103_update_device(dev);
  311. int rpm = 0;
  312. /* high byte of 0xff indicates disabled so return 0 */
  313. if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
  314. rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
  315. / data->fan_target;
  316. return sprintf(buf, "%d\n", rpm);
  317. }
  318. static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
  319. const char *buf, size_t count)
  320. {
  321. struct emc2103_data *data = emc2103_update_device(dev);
  322. struct i2c_client *client = to_i2c_client(dev);
  323. long rpm_target;
  324. int result = strict_strtol(buf, 10, &rpm_target);
  325. if (result < 0)
  326. return -EINVAL;
  327. /* Datasheet states 16384 as maximum RPM target (table 3.2) */
  328. if ((rpm_target < 0) || (rpm_target > 16384))
  329. return -EINVAL;
  330. mutex_lock(&data->update_lock);
  331. if (rpm_target == 0)
  332. data->fan_target = 0x1fff;
  333. else
  334. data->fan_target = SENSORS_LIMIT(
  335. (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
  336. 0, 0x1fff);
  337. write_fan_target_to_i2c(client, data->fan_target);
  338. mutex_unlock(&data->update_lock);
  339. return count;
  340. }
  341. static ssize_t
  342. show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
  343. {
  344. struct emc2103_data *data = emc2103_update_device(dev);
  345. bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
  346. return sprintf(buf, "%d\n", fault ? 1 : 0);
  347. }
  348. static ssize_t
  349. show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
  350. {
  351. struct emc2103_data *data = emc2103_update_device(dev);
  352. return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
  353. }
  354. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
  355. const char *buf, size_t count)
  356. {
  357. struct i2c_client *client = to_i2c_client(dev);
  358. struct emc2103_data *data = i2c_get_clientdata(client);
  359. long new_value;
  360. u8 conf_reg;
  361. int result = strict_strtol(buf, 10, &new_value);
  362. if (result < 0)
  363. return -EINVAL;
  364. mutex_lock(&data->update_lock);
  365. switch (new_value) {
  366. case 0:
  367. data->fan_rpm_control = false;
  368. break;
  369. case 3:
  370. data->fan_rpm_control = true;
  371. break;
  372. default:
  373. mutex_unlock(&data->update_lock);
  374. return -EINVAL;
  375. }
  376. read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
  377. if (data->fan_rpm_control)
  378. conf_reg |= 0x80;
  379. else
  380. conf_reg &= ~0x80;
  381. i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
  382. mutex_unlock(&data->update_lock);
  383. return count;
  384. }
  385. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  386. static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp_min,
  387. set_temp_min, 0);
  388. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
  389. set_temp_max, 0);
  390. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
  391. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm,
  392. NULL, 0);
  393. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm,
  394. NULL, 0);
  395. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  396. static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, show_temp_min,
  397. set_temp_min, 1);
  398. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
  399. set_temp_max, 1);
  400. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
  401. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm,
  402. NULL, 1);
  403. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm,
  404. NULL, 1);
  405. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  406. static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, show_temp_min,
  407. set_temp_min, 2);
  408. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
  409. set_temp_max, 2);
  410. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
  411. static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm,
  412. NULL, 2);
  413. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm,
  414. NULL, 2);
  415. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  416. static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, show_temp_min,
  417. set_temp_min, 3);
  418. static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
  419. set_temp_max, 3);
  420. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
  421. static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_temp_min_alarm,
  422. NULL, 3);
  423. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_temp_max_alarm,
  424. NULL, 3);
  425. static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
  426. static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div, set_fan_div);
  427. static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_fan_target,
  428. set_fan_target);
  429. static DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL);
  430. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  431. set_pwm_enable);
  432. /* sensors present on all models */
  433. static struct attribute *emc2103_attributes[] = {
  434. &sensor_dev_attr_temp1_input.dev_attr.attr,
  435. &sensor_dev_attr_temp1_min.dev_attr.attr,
  436. &sensor_dev_attr_temp1_max.dev_attr.attr,
  437. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  438. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  439. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  440. &sensor_dev_attr_temp2_input.dev_attr.attr,
  441. &sensor_dev_attr_temp2_min.dev_attr.attr,
  442. &sensor_dev_attr_temp2_max.dev_attr.attr,
  443. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  444. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  445. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  446. &dev_attr_fan1_input.attr,
  447. &dev_attr_fan1_div.attr,
  448. &dev_attr_fan1_target.attr,
  449. &dev_attr_fan1_fault.attr,
  450. &dev_attr_pwm1_enable.attr,
  451. NULL
  452. };
  453. /* extra temperature sensors only present on 2103-2 and 2103-4 */
  454. static struct attribute *emc2103_attributes_temp3[] = {
  455. &sensor_dev_attr_temp3_input.dev_attr.attr,
  456. &sensor_dev_attr_temp3_min.dev_attr.attr,
  457. &sensor_dev_attr_temp3_max.dev_attr.attr,
  458. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  459. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  460. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  461. NULL
  462. };
  463. /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
  464. static struct attribute *emc2103_attributes_temp4[] = {
  465. &sensor_dev_attr_temp4_input.dev_attr.attr,
  466. &sensor_dev_attr_temp4_min.dev_attr.attr,
  467. &sensor_dev_attr_temp4_max.dev_attr.attr,
  468. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  469. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  470. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  471. NULL
  472. };
  473. static const struct attribute_group emc2103_group = {
  474. .attrs = emc2103_attributes,
  475. };
  476. static const struct attribute_group emc2103_temp3_group = {
  477. .attrs = emc2103_attributes_temp3,
  478. };
  479. static const struct attribute_group emc2103_temp4_group = {
  480. .attrs = emc2103_attributes_temp4,
  481. };
  482. static int
  483. emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id)
  484. {
  485. struct emc2103_data *data;
  486. int status;
  487. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  488. return -EIO;
  489. data = kzalloc(sizeof(struct emc2103_data), GFP_KERNEL);
  490. if (!data)
  491. return -ENOMEM;
  492. i2c_set_clientdata(client, data);
  493. mutex_init(&data->update_lock);
  494. /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
  495. status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
  496. if (status == 0x24) {
  497. /* 2103-1 only has 1 external diode */
  498. data->temp_count = 2;
  499. } else {
  500. /* 2103-2 and 2103-4 have 3 or 4 external diodes */
  501. status = i2c_smbus_read_byte_data(client, REG_CONF1);
  502. if (status < 0) {
  503. dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
  504. status);
  505. goto exit_free;
  506. }
  507. /* detect current state of hardware */
  508. data->temp_count = (status & 0x01) ? 4 : 3;
  509. /* force APD state if module parameter is set */
  510. if (apd == 0) {
  511. /* force APD mode off */
  512. data->temp_count = 3;
  513. status &= ~(0x01);
  514. i2c_smbus_write_byte_data(client, REG_CONF1, status);
  515. } else if (apd == 1) {
  516. /* force APD mode on */
  517. data->temp_count = 4;
  518. status |= 0x01;
  519. i2c_smbus_write_byte_data(client, REG_CONF1, status);
  520. }
  521. }
  522. /* Register sysfs hooks */
  523. status = sysfs_create_group(&client->dev.kobj, &emc2103_group);
  524. if (status)
  525. goto exit_free;
  526. if (data->temp_count >= 3) {
  527. status = sysfs_create_group(&client->dev.kobj,
  528. &emc2103_temp3_group);
  529. if (status)
  530. goto exit_remove;
  531. }
  532. if (data->temp_count == 4) {
  533. status = sysfs_create_group(&client->dev.kobj,
  534. &emc2103_temp4_group);
  535. if (status)
  536. goto exit_remove_temp3;
  537. }
  538. data->hwmon_dev = hwmon_device_register(&client->dev);
  539. if (IS_ERR(data->hwmon_dev)) {
  540. status = PTR_ERR(data->hwmon_dev);
  541. goto exit_remove_temp4;
  542. }
  543. dev_info(&client->dev, "%s: sensor '%s'\n",
  544. dev_name(data->hwmon_dev), client->name);
  545. return 0;
  546. exit_remove_temp4:
  547. if (data->temp_count == 4)
  548. sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
  549. exit_remove_temp3:
  550. if (data->temp_count >= 3)
  551. sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
  552. exit_remove:
  553. sysfs_remove_group(&client->dev.kobj, &emc2103_group);
  554. exit_free:
  555. kfree(data);
  556. return status;
  557. }
  558. static int emc2103_remove(struct i2c_client *client)
  559. {
  560. struct emc2103_data *data = i2c_get_clientdata(client);
  561. hwmon_device_unregister(data->hwmon_dev);
  562. if (data->temp_count == 4)
  563. sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
  564. if (data->temp_count >= 3)
  565. sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
  566. sysfs_remove_group(&client->dev.kobj, &emc2103_group);
  567. kfree(data);
  568. return 0;
  569. }
  570. static const struct i2c_device_id emc2103_ids[] = {
  571. { "emc2103", 0, },
  572. { /* LIST END */ }
  573. };
  574. MODULE_DEVICE_TABLE(i2c, emc2103_ids);
  575. /* Return 0 if detection is successful, -ENODEV otherwise */
  576. static int
  577. emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
  578. {
  579. struct i2c_adapter *adapter = new_client->adapter;
  580. int manufacturer, product;
  581. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  582. return -ENODEV;
  583. manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
  584. if (manufacturer != 0x5D)
  585. return -ENODEV;
  586. product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
  587. if ((product != 0x24) && (product != 0x26))
  588. return -ENODEV;
  589. strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
  590. return 0;
  591. }
  592. static struct i2c_driver emc2103_driver = {
  593. .class = I2C_CLASS_HWMON,
  594. .driver = {
  595. .name = "emc2103",
  596. },
  597. .probe = emc2103_probe,
  598. .remove = emc2103_remove,
  599. .id_table = emc2103_ids,
  600. .detect = emc2103_detect,
  601. .address_list = normal_i2c,
  602. };
  603. static int __init sensors_emc2103_init(void)
  604. {
  605. return i2c_add_driver(&emc2103_driver);
  606. }
  607. static void __exit sensors_emc2103_exit(void)
  608. {
  609. i2c_del_driver(&emc2103_driver);
  610. }
  611. MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
  612. MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
  613. MODULE_LICENSE("GPL");
  614. module_init(sensors_emc2103_init);
  615. module_exit(sensors_emc2103_exit);