emc2103.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 result;
  213. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
  214. mutex_lock(&data->update_lock);
  215. data->temp_min[nr] = val;
  216. i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
  217. mutex_unlock(&data->update_lock);
  218. return count;
  219. }
  220. static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
  221. const char *buf, size_t count)
  222. {
  223. int nr = to_sensor_dev_attr(da)->index;
  224. struct i2c_client *client = to_i2c_client(dev);
  225. struct emc2103_data *data = i2c_get_clientdata(client);
  226. long val;
  227. int result = kstrtol(buf, 10, &val);
  228. if (result < 0)
  229. return result;
  230. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
  231. mutex_lock(&data->update_lock);
  232. data->temp_max[nr] = val;
  233. i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
  234. mutex_unlock(&data->update_lock);
  235. return count;
  236. }
  237. static ssize_t
  238. show_fan(struct device *dev, struct device_attribute *da, char *buf)
  239. {
  240. struct emc2103_data *data = emc2103_update_device(dev);
  241. int rpm = 0;
  242. if (data->fan_tach != 0)
  243. rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
  244. return sprintf(buf, "%d\n", rpm);
  245. }
  246. static ssize_t
  247. show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
  248. {
  249. struct emc2103_data *data = emc2103_update_device(dev);
  250. int fan_div = 8 / data->fan_multiplier;
  251. return sprintf(buf, "%d\n", fan_div);
  252. }
  253. /*
  254. * Note: we also update the fan target here, because its value is
  255. * determined in part by the fan clock divider. This follows the principle
  256. * of least surprise; the user doesn't expect the fan target to change just
  257. * because the divider changed.
  258. */
  259. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  260. const char *buf, size_t count)
  261. {
  262. struct emc2103_data *data = emc2103_update_device(dev);
  263. struct i2c_client *client = to_i2c_client(dev);
  264. int new_range_bits, old_div = 8 / data->fan_multiplier;
  265. long new_div;
  266. int status = kstrtol(buf, 10, &new_div);
  267. if (status < 0)
  268. return status;
  269. if (new_div == old_div) /* No change */
  270. return count;
  271. switch (new_div) {
  272. case 1:
  273. new_range_bits = 3;
  274. break;
  275. case 2:
  276. new_range_bits = 2;
  277. break;
  278. case 4:
  279. new_range_bits = 1;
  280. break;
  281. case 8:
  282. new_range_bits = 0;
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. mutex_lock(&data->update_lock);
  288. status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
  289. if (status < 0) {
  290. dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
  291. REG_FAN_CONF1, status);
  292. mutex_unlock(&data->update_lock);
  293. return status;
  294. }
  295. status &= 0x9F;
  296. status |= (new_range_bits << 5);
  297. i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
  298. data->fan_multiplier = 8 / new_div;
  299. /* update fan target if high byte is not disabled */
  300. if ((data->fan_target & 0x1fe0) != 0x1fe0) {
  301. u16 new_target = (data->fan_target * old_div) / new_div;
  302. data->fan_target = min(new_target, (u16)0x1fff);
  303. write_fan_target_to_i2c(client, data->fan_target);
  304. }
  305. /* invalidate data to force re-read from hardware */
  306. data->valid = false;
  307. mutex_unlock(&data->update_lock);
  308. return count;
  309. }
  310. static ssize_t
  311. show_fan_target(struct device *dev, struct device_attribute *da, char *buf)
  312. {
  313. struct emc2103_data *data = emc2103_update_device(dev);
  314. int rpm = 0;
  315. /* high byte of 0xff indicates disabled so return 0 */
  316. if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
  317. rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
  318. / data->fan_target;
  319. return sprintf(buf, "%d\n", rpm);
  320. }
  321. static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
  322. const char *buf, size_t count)
  323. {
  324. struct emc2103_data *data = emc2103_update_device(dev);
  325. struct i2c_client *client = to_i2c_client(dev);
  326. unsigned long rpm_target;
  327. int result = kstrtoul(buf, 10, &rpm_target);
  328. if (result < 0)
  329. return result;
  330. /* Datasheet states 16384 as maximum RPM target (table 3.2) */
  331. rpm_target = clamp_val(rpm_target, 0, 16384);
  332. mutex_lock(&data->update_lock);
  333. if (rpm_target == 0)
  334. data->fan_target = 0x1fff;
  335. else
  336. data->fan_target = clamp_val(
  337. (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
  338. 0, 0x1fff);
  339. write_fan_target_to_i2c(client, data->fan_target);
  340. mutex_unlock(&data->update_lock);
  341. return count;
  342. }
  343. static ssize_t
  344. show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
  345. {
  346. struct emc2103_data *data = emc2103_update_device(dev);
  347. bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
  348. return sprintf(buf, "%d\n", fault ? 1 : 0);
  349. }
  350. static ssize_t
  351. show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
  352. {
  353. struct emc2103_data *data = emc2103_update_device(dev);
  354. return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
  355. }
  356. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
  357. const char *buf, size_t count)
  358. {
  359. struct i2c_client *client = to_i2c_client(dev);
  360. struct emc2103_data *data = i2c_get_clientdata(client);
  361. long new_value;
  362. u8 conf_reg;
  363. int result = kstrtol(buf, 10, &new_value);
  364. if (result < 0)
  365. return result;
  366. mutex_lock(&data->update_lock);
  367. switch (new_value) {
  368. case 0:
  369. data->fan_rpm_control = false;
  370. break;
  371. case 3:
  372. data->fan_rpm_control = true;
  373. break;
  374. default:
  375. count = -EINVAL;
  376. goto err;
  377. }
  378. result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
  379. if (result) {
  380. count = result;
  381. goto err;
  382. }
  383. if (data->fan_rpm_control)
  384. conf_reg |= 0x80;
  385. else
  386. conf_reg &= ~0x80;
  387. i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
  388. err:
  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 = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
  497. GFP_KERNEL);
  498. if (!data)
  499. return -ENOMEM;
  500. i2c_set_clientdata(client, data);
  501. mutex_init(&data->update_lock);
  502. /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
  503. status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
  504. if (status == 0x24) {
  505. /* 2103-1 only has 1 external diode */
  506. data->temp_count = 2;
  507. } else {
  508. /* 2103-2 and 2103-4 have 3 or 4 external diodes */
  509. status = i2c_smbus_read_byte_data(client, REG_CONF1);
  510. if (status < 0) {
  511. dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
  512. status);
  513. return status;
  514. }
  515. /* detect current state of hardware */
  516. data->temp_count = (status & 0x01) ? 4 : 3;
  517. /* force APD state if module parameter is set */
  518. if (apd == 0) {
  519. /* force APD mode off */
  520. data->temp_count = 3;
  521. status &= ~(0x01);
  522. i2c_smbus_write_byte_data(client, REG_CONF1, status);
  523. } else if (apd == 1) {
  524. /* force APD mode on */
  525. data->temp_count = 4;
  526. status |= 0x01;
  527. i2c_smbus_write_byte_data(client, REG_CONF1, status);
  528. }
  529. }
  530. /* Register sysfs hooks */
  531. status = sysfs_create_group(&client->dev.kobj, &emc2103_group);
  532. if (status)
  533. return status;
  534. if (data->temp_count >= 3) {
  535. status = sysfs_create_group(&client->dev.kobj,
  536. &emc2103_temp3_group);
  537. if (status)
  538. goto exit_remove;
  539. }
  540. if (data->temp_count == 4) {
  541. status = sysfs_create_group(&client->dev.kobj,
  542. &emc2103_temp4_group);
  543. if (status)
  544. goto exit_remove_temp3;
  545. }
  546. data->hwmon_dev = hwmon_device_register(&client->dev);
  547. if (IS_ERR(data->hwmon_dev)) {
  548. status = PTR_ERR(data->hwmon_dev);
  549. goto exit_remove_temp4;
  550. }
  551. dev_info(&client->dev, "%s: sensor '%s'\n",
  552. dev_name(data->hwmon_dev), client->name);
  553. return 0;
  554. exit_remove_temp4:
  555. if (data->temp_count == 4)
  556. sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
  557. exit_remove_temp3:
  558. if (data->temp_count >= 3)
  559. sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
  560. exit_remove:
  561. sysfs_remove_group(&client->dev.kobj, &emc2103_group);
  562. return status;
  563. }
  564. static int emc2103_remove(struct i2c_client *client)
  565. {
  566. struct emc2103_data *data = i2c_get_clientdata(client);
  567. hwmon_device_unregister(data->hwmon_dev);
  568. if (data->temp_count == 4)
  569. sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
  570. if (data->temp_count >= 3)
  571. sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
  572. sysfs_remove_group(&client->dev.kobj, &emc2103_group);
  573. return 0;
  574. }
  575. static const struct i2c_device_id emc2103_ids[] = {
  576. { "emc2103", 0, },
  577. { /* LIST END */ }
  578. };
  579. MODULE_DEVICE_TABLE(i2c, emc2103_ids);
  580. /* Return 0 if detection is successful, -ENODEV otherwise */
  581. static int
  582. emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
  583. {
  584. struct i2c_adapter *adapter = new_client->adapter;
  585. int manufacturer, product;
  586. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  587. return -ENODEV;
  588. manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
  589. if (manufacturer != 0x5D)
  590. return -ENODEV;
  591. product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
  592. if ((product != 0x24) && (product != 0x26))
  593. return -ENODEV;
  594. strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
  595. return 0;
  596. }
  597. static struct i2c_driver emc2103_driver = {
  598. .class = I2C_CLASS_HWMON,
  599. .driver = {
  600. .name = "emc2103",
  601. },
  602. .probe = emc2103_probe,
  603. .remove = emc2103_remove,
  604. .id_table = emc2103_ids,
  605. .detect = emc2103_detect,
  606. .address_list = normal_i2c,
  607. };
  608. module_i2c_driver(emc2103_driver);
  609. MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
  610. MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
  611. MODULE_LICENSE("GPL");