emc2103.c 20 KB

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