emc6w201.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * emc6w201.c - Hardware monitoring driver for the SMSC EMC6W201
  3. * Copyright (C) 2011 Jean Delvare <khali@linux-fr.org>
  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/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/i2c.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. /*
  30. * Addresses to scan
  31. */
  32. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  33. /*
  34. * The EMC6W201 registers
  35. */
  36. #define EMC6W201_REG_IN(nr) (0x20 + (nr))
  37. #define EMC6W201_REG_TEMP(nr) (0x26 + (nr))
  38. #define EMC6W201_REG_FAN(nr) (0x2C + (nr) * 2)
  39. #define EMC6W201_REG_COMPANY 0x3E
  40. #define EMC6W201_REG_VERSTEP 0x3F
  41. #define EMC6W201_REG_CONFIG 0x40
  42. #define EMC6W201_REG_IN_LOW(nr) (0x4A + (nr) * 2)
  43. #define EMC6W201_REG_IN_HIGH(nr) (0x4B + (nr) * 2)
  44. #define EMC6W201_REG_TEMP_LOW(nr) (0x56 + (nr) * 2)
  45. #define EMC6W201_REG_TEMP_HIGH(nr) (0x57 + (nr) * 2)
  46. #define EMC6W201_REG_FAN_MIN(nr) (0x62 + (nr) * 2)
  47. enum { input, min, max } subfeature;
  48. /*
  49. * Per-device data
  50. */
  51. struct emc6w201_data {
  52. struct device *hwmon_dev;
  53. struct mutex update_lock;
  54. char valid; /* zero until following fields are valid */
  55. unsigned long last_updated; /* in jiffies */
  56. /* registers values */
  57. u8 in[3][6];
  58. s8 temp[3][6];
  59. u16 fan[2][5];
  60. };
  61. /*
  62. * Combine LSB and MSB registers in a single value
  63. * Locking: must be called with data->update_lock held
  64. */
  65. static u16 emc6w201_read16(struct i2c_client *client, u8 reg)
  66. {
  67. int lsb, msb;
  68. lsb = i2c_smbus_read_byte_data(client, reg);
  69. msb = i2c_smbus_read_byte_data(client, reg + 1);
  70. if (unlikely(lsb < 0 || msb < 0)) {
  71. dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
  72. 16, "read", reg);
  73. return 0xFFFF; /* Arbitrary value */
  74. }
  75. return (msb << 8) | lsb;
  76. }
  77. /*
  78. * Write 16-bit value to LSB and MSB registers
  79. * Locking: must be called with data->update_lock held
  80. */
  81. static int emc6w201_write16(struct i2c_client *client, u8 reg, u16 val)
  82. {
  83. int err;
  84. err = i2c_smbus_write_byte_data(client, reg, val & 0xff);
  85. if (likely(!err))
  86. err = i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
  87. if (unlikely(err < 0))
  88. dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
  89. 16, "write", reg);
  90. return err;
  91. }
  92. /* Read 8-bit value from register */
  93. static u8 emc6w201_read8(struct i2c_client *client, u8 reg)
  94. {
  95. int val;
  96. val = i2c_smbus_read_byte_data(client, reg);
  97. if (unlikely(val < 0)) {
  98. dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
  99. 8, "read", reg);
  100. return 0x00; /* Arbitrary value */
  101. }
  102. return val;
  103. }
  104. /* Write 8-bit value to register */
  105. static int emc6w201_write8(struct i2c_client *client, u8 reg, u8 val)
  106. {
  107. int err;
  108. err = i2c_smbus_write_byte_data(client, reg, val);
  109. if (unlikely(err < 0))
  110. dev_err(&client->dev, "%d-bit %s failed at 0x%02x\n",
  111. 8, "write", reg);
  112. return err;
  113. }
  114. static struct emc6w201_data *emc6w201_update_device(struct device *dev)
  115. {
  116. struct i2c_client *client = to_i2c_client(dev);
  117. struct emc6w201_data *data = i2c_get_clientdata(client);
  118. int nr;
  119. mutex_lock(&data->update_lock);
  120. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  121. for (nr = 0; nr < 6; nr++) {
  122. data->in[input][nr] =
  123. emc6w201_read8(client,
  124. EMC6W201_REG_IN(nr));
  125. data->in[min][nr] =
  126. emc6w201_read8(client,
  127. EMC6W201_REG_IN_LOW(nr));
  128. data->in[max][nr] =
  129. emc6w201_read8(client,
  130. EMC6W201_REG_IN_HIGH(nr));
  131. }
  132. for (nr = 0; nr < 6; nr++) {
  133. data->temp[input][nr] =
  134. emc6w201_read8(client,
  135. EMC6W201_REG_TEMP(nr));
  136. data->temp[min][nr] =
  137. emc6w201_read8(client,
  138. EMC6W201_REG_TEMP_LOW(nr));
  139. data->temp[max][nr] =
  140. emc6w201_read8(client,
  141. EMC6W201_REG_TEMP_HIGH(nr));
  142. }
  143. for (nr = 0; nr < 5; nr++) {
  144. data->fan[input][nr] =
  145. emc6w201_read16(client,
  146. EMC6W201_REG_FAN(nr));
  147. data->fan[min][nr] =
  148. emc6w201_read16(client,
  149. EMC6W201_REG_FAN_MIN(nr));
  150. }
  151. data->last_updated = jiffies;
  152. data->valid = 1;
  153. }
  154. mutex_unlock(&data->update_lock);
  155. return data;
  156. }
  157. /*
  158. * Sysfs callback functions
  159. */
  160. static const u16 nominal_mv[6] = { 2500, 1500, 3300, 5000, 1500, 1500 };
  161. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  162. char *buf)
  163. {
  164. struct emc6w201_data *data = emc6w201_update_device(dev);
  165. int sf = to_sensor_dev_attr_2(devattr)->index;
  166. int nr = to_sensor_dev_attr_2(devattr)->nr;
  167. return sprintf(buf, "%u\n",
  168. (unsigned)data->in[sf][nr] * nominal_mv[nr] / 0xC0);
  169. }
  170. static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
  171. const char *buf, size_t count)
  172. {
  173. struct i2c_client *client = to_i2c_client(dev);
  174. struct emc6w201_data *data = i2c_get_clientdata(client);
  175. int sf = to_sensor_dev_attr_2(devattr)->index;
  176. int nr = to_sensor_dev_attr_2(devattr)->nr;
  177. int err;
  178. long val;
  179. u8 reg;
  180. err = strict_strtol(buf, 10, &val);
  181. if (err < 0)
  182. return err;
  183. val = DIV_ROUND_CLOSEST(val * 0xC0, nominal_mv[nr]);
  184. reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr)
  185. : EMC6W201_REG_IN_HIGH(nr);
  186. mutex_lock(&data->update_lock);
  187. data->in[sf][nr] = SENSORS_LIMIT(val, 0, 255);
  188. err = emc6w201_write8(client, reg, data->in[sf][nr]);
  189. mutex_unlock(&data->update_lock);
  190. return err < 0 ? err : count;
  191. }
  192. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  193. char *buf)
  194. {
  195. struct emc6w201_data *data = emc6w201_update_device(dev);
  196. int sf = to_sensor_dev_attr_2(devattr)->index;
  197. int nr = to_sensor_dev_attr_2(devattr)->nr;
  198. return sprintf(buf, "%d\n", (int)data->temp[sf][nr] * 1000);
  199. }
  200. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  201. const char *buf, size_t count)
  202. {
  203. struct i2c_client *client = to_i2c_client(dev);
  204. struct emc6w201_data *data = i2c_get_clientdata(client);
  205. int sf = to_sensor_dev_attr_2(devattr)->index;
  206. int nr = to_sensor_dev_attr_2(devattr)->nr;
  207. int err;
  208. long val;
  209. u8 reg;
  210. err = strict_strtol(buf, 10, &val);
  211. if (err < 0)
  212. return err;
  213. val /= 1000;
  214. reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
  215. : EMC6W201_REG_TEMP_HIGH(nr);
  216. mutex_lock(&data->update_lock);
  217. data->temp[sf][nr] = SENSORS_LIMIT(val, -127, 128);
  218. err = emc6w201_write8(client, reg, data->temp[sf][nr]);
  219. mutex_unlock(&data->update_lock);
  220. return err < 0 ? err : count;
  221. }
  222. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  223. char *buf)
  224. {
  225. struct emc6w201_data *data = emc6w201_update_device(dev);
  226. int sf = to_sensor_dev_attr_2(devattr)->index;
  227. int nr = to_sensor_dev_attr_2(devattr)->nr;
  228. unsigned rpm;
  229. if (data->fan[sf][nr] == 0 || data->fan[sf][nr] == 0xFFFF)
  230. rpm = 0;
  231. else
  232. rpm = 5400000U / data->fan[sf][nr];
  233. return sprintf(buf, "%u\n", rpm);
  234. }
  235. static ssize_t set_fan(struct device *dev, struct device_attribute *devattr,
  236. const char *buf, size_t count)
  237. {
  238. struct i2c_client *client = to_i2c_client(dev);
  239. struct emc6w201_data *data = i2c_get_clientdata(client);
  240. int sf = to_sensor_dev_attr_2(devattr)->index;
  241. int nr = to_sensor_dev_attr_2(devattr)->nr;
  242. int err;
  243. unsigned long val;
  244. err = strict_strtoul(buf, 10, &val);
  245. if (err < 0)
  246. return err;
  247. if (val == 0) {
  248. val = 0xFFFF;
  249. } else {
  250. val = DIV_ROUND_CLOSEST(5400000U, val);
  251. val = SENSORS_LIMIT(val, 0, 0xFFFE);
  252. }
  253. mutex_lock(&data->update_lock);
  254. data->fan[sf][nr] = val;
  255. err = emc6w201_write16(client, EMC6W201_REG_FAN_MIN(nr),
  256. data->fan[sf][nr]);
  257. mutex_unlock(&data->update_lock);
  258. return err < 0 ? err : count;
  259. }
  260. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, input);
  261. static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
  262. 0, min);
  263. static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
  264. 0, max);
  265. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, input);
  266. static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
  267. 1, min);
  268. static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
  269. 1, max);
  270. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, input);
  271. static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
  272. 2, min);
  273. static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
  274. 2, max);
  275. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, input);
  276. static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
  277. 3, min);
  278. static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
  279. 3, max);
  280. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, input);
  281. static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
  282. 4, min);
  283. static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
  284. 4, max);
  285. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, input);
  286. static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
  287. 5, min);
  288. static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
  289. 5, max);
  290. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, input);
  291. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  292. 0, min);
  293. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  294. 0, max);
  295. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, input);
  296. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  297. 1, min);
  298. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  299. 1, max);
  300. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, input);
  301. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  302. 2, min);
  303. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  304. 2, max);
  305. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, input);
  306. static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  307. 3, min);
  308. static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  309. 3, max);
  310. static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, input);
  311. static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  312. 4, min);
  313. static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  314. 4, max);
  315. static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, input);
  316. static SENSOR_DEVICE_ATTR_2(temp6_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  317. 5, min);
  318. static SENSOR_DEVICE_ATTR_2(temp6_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  319. 5, max);
  320. static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, input);
  321. static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  322. 0, min);
  323. static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, input);
  324. static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  325. 1, min);
  326. static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, input);
  327. static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  328. 2, min);
  329. static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, input);
  330. static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  331. 3, min);
  332. static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, input);
  333. static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  334. 4, min);
  335. static struct attribute *emc6w201_attributes[] = {
  336. &sensor_dev_attr_in0_input.dev_attr.attr,
  337. &sensor_dev_attr_in0_min.dev_attr.attr,
  338. &sensor_dev_attr_in0_max.dev_attr.attr,
  339. &sensor_dev_attr_in1_input.dev_attr.attr,
  340. &sensor_dev_attr_in1_min.dev_attr.attr,
  341. &sensor_dev_attr_in1_max.dev_attr.attr,
  342. &sensor_dev_attr_in2_input.dev_attr.attr,
  343. &sensor_dev_attr_in2_min.dev_attr.attr,
  344. &sensor_dev_attr_in2_max.dev_attr.attr,
  345. &sensor_dev_attr_in3_input.dev_attr.attr,
  346. &sensor_dev_attr_in3_min.dev_attr.attr,
  347. &sensor_dev_attr_in3_max.dev_attr.attr,
  348. &sensor_dev_attr_in4_input.dev_attr.attr,
  349. &sensor_dev_attr_in4_min.dev_attr.attr,
  350. &sensor_dev_attr_in4_max.dev_attr.attr,
  351. &sensor_dev_attr_in5_input.dev_attr.attr,
  352. &sensor_dev_attr_in5_min.dev_attr.attr,
  353. &sensor_dev_attr_in5_max.dev_attr.attr,
  354. &sensor_dev_attr_temp1_input.dev_attr.attr,
  355. &sensor_dev_attr_temp1_min.dev_attr.attr,
  356. &sensor_dev_attr_temp1_max.dev_attr.attr,
  357. &sensor_dev_attr_temp2_input.dev_attr.attr,
  358. &sensor_dev_attr_temp2_min.dev_attr.attr,
  359. &sensor_dev_attr_temp2_max.dev_attr.attr,
  360. &sensor_dev_attr_temp3_input.dev_attr.attr,
  361. &sensor_dev_attr_temp3_min.dev_attr.attr,
  362. &sensor_dev_attr_temp3_max.dev_attr.attr,
  363. &sensor_dev_attr_temp4_input.dev_attr.attr,
  364. &sensor_dev_attr_temp4_min.dev_attr.attr,
  365. &sensor_dev_attr_temp4_max.dev_attr.attr,
  366. &sensor_dev_attr_temp5_input.dev_attr.attr,
  367. &sensor_dev_attr_temp5_min.dev_attr.attr,
  368. &sensor_dev_attr_temp5_max.dev_attr.attr,
  369. &sensor_dev_attr_temp6_input.dev_attr.attr,
  370. &sensor_dev_attr_temp6_min.dev_attr.attr,
  371. &sensor_dev_attr_temp6_max.dev_attr.attr,
  372. &sensor_dev_attr_fan1_input.dev_attr.attr,
  373. &sensor_dev_attr_fan1_min.dev_attr.attr,
  374. &sensor_dev_attr_fan2_input.dev_attr.attr,
  375. &sensor_dev_attr_fan2_min.dev_attr.attr,
  376. &sensor_dev_attr_fan3_input.dev_attr.attr,
  377. &sensor_dev_attr_fan3_min.dev_attr.attr,
  378. &sensor_dev_attr_fan4_input.dev_attr.attr,
  379. &sensor_dev_attr_fan4_min.dev_attr.attr,
  380. &sensor_dev_attr_fan5_input.dev_attr.attr,
  381. &sensor_dev_attr_fan5_min.dev_attr.attr,
  382. NULL
  383. };
  384. static const struct attribute_group emc6w201_group = {
  385. .attrs = emc6w201_attributes,
  386. };
  387. /*
  388. * Driver interface
  389. */
  390. /* Return 0 if detection is successful, -ENODEV otherwise */
  391. static int emc6w201_detect(struct i2c_client *client,
  392. struct i2c_board_info *info)
  393. {
  394. struct i2c_adapter *adapter = client->adapter;
  395. int company, verstep, config;
  396. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  397. return -ENODEV;
  398. /* Identification */
  399. company = i2c_smbus_read_byte_data(client, EMC6W201_REG_COMPANY);
  400. if (company != 0x5C)
  401. return -ENODEV;
  402. verstep = i2c_smbus_read_byte_data(client, EMC6W201_REG_VERSTEP);
  403. if (verstep < 0 || (verstep & 0xF0) != 0xB0)
  404. return -ENODEV;
  405. if ((verstep & 0x0F) > 2) {
  406. dev_dbg(&client->dev, "Unknwown EMC6W201 stepping %d\n",
  407. verstep & 0x0F);
  408. return -ENODEV;
  409. }
  410. /* Check configuration */
  411. config = i2c_smbus_read_byte_data(client, EMC6W201_REG_CONFIG);
  412. if (config < 0 || (config & 0xF4) != 0x04)
  413. return -ENODEV;
  414. if (!(config & 0x01)) {
  415. dev_err(&client->dev, "Monitoring not enabled\n");
  416. return -ENODEV;
  417. }
  418. strlcpy(info->type, "emc6w201", I2C_NAME_SIZE);
  419. return 0;
  420. }
  421. static int emc6w201_probe(struct i2c_client *client,
  422. const struct i2c_device_id *id)
  423. {
  424. struct emc6w201_data *data;
  425. int err;
  426. data = kzalloc(sizeof(struct emc6w201_data), GFP_KERNEL);
  427. if (!data) {
  428. err = -ENOMEM;
  429. goto exit;
  430. }
  431. i2c_set_clientdata(client, data);
  432. mutex_init(&data->update_lock);
  433. /* Create sysfs attribute */
  434. err = sysfs_create_group(&client->dev.kobj, &emc6w201_group);
  435. if (err)
  436. goto exit_free;
  437. /* Expose as a hwmon device */
  438. data->hwmon_dev = hwmon_device_register(&client->dev);
  439. if (IS_ERR(data->hwmon_dev)) {
  440. err = PTR_ERR(data->hwmon_dev);
  441. goto exit_remove;
  442. }
  443. return 0;
  444. exit_remove:
  445. sysfs_remove_group(&client->dev.kobj, &emc6w201_group);
  446. exit_free:
  447. kfree(data);
  448. exit:
  449. return err;
  450. }
  451. static int emc6w201_remove(struct i2c_client *client)
  452. {
  453. struct emc6w201_data *data = i2c_get_clientdata(client);
  454. hwmon_device_unregister(data->hwmon_dev);
  455. sysfs_remove_group(&client->dev.kobj, &emc6w201_group);
  456. kfree(data);
  457. return 0;
  458. }
  459. static const struct i2c_device_id emc6w201_id[] = {
  460. { "emc6w201", 0 },
  461. { }
  462. };
  463. MODULE_DEVICE_TABLE(i2c, emc6w201_id);
  464. static struct i2c_driver emc6w201_driver = {
  465. .class = I2C_CLASS_HWMON,
  466. .driver = {
  467. .name = "emc6w201",
  468. },
  469. .probe = emc6w201_probe,
  470. .remove = emc6w201_remove,
  471. .id_table = emc6w201_id,
  472. .detect = emc6w201_detect,
  473. .address_list = normal_i2c,
  474. };
  475. static int __init sensors_emc6w201_init(void)
  476. {
  477. return i2c_add_driver(&emc6w201_driver);
  478. }
  479. module_init(sensors_emc6w201_init);
  480. static void __exit sensors_emc6w201_exit(void)
  481. {
  482. i2c_del_driver(&emc6w201_driver);
  483. }
  484. module_exit(sensors_emc6w201_exit);
  485. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  486. MODULE_DESCRIPTION("SMSC EMC6W201 hardware monitoring driver");
  487. MODULE_LICENSE("GPL");