emc2103.c 21 KB

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