lm95234.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Driver for Texas Instruments / National Semiconductor LM95234
  3. *
  4. * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
  5. *
  6. * Derived from lm95241.c
  7. * Copyright (C) 2008, 2010 Davide Rizzo <elpa.rizzo@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  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. #include <linux/sysfs.h>
  29. #define DRVNAME "lm95234"
  30. static const unsigned short normal_i2c[] = { 0x18, 0x4d, 0x4e, I2C_CLIENT_END };
  31. /* LM95234 registers */
  32. #define LM95234_REG_MAN_ID 0xFE
  33. #define LM95234_REG_CHIP_ID 0xFF
  34. #define LM95234_REG_STATUS 0x02
  35. #define LM95234_REG_CONFIG 0x03
  36. #define LM95234_REG_CONVRATE 0x04
  37. #define LM95234_REG_STS_FAULT 0x07
  38. #define LM95234_REG_STS_TCRIT1 0x08
  39. #define LM95234_REG_STS_TCRIT2 0x09
  40. #define LM95234_REG_TEMPH(x) ((x) + 0x10)
  41. #define LM95234_REG_TEMPL(x) ((x) + 0x20)
  42. #define LM95234_REG_UTEMPH(x) ((x) + 0x19) /* Remote only */
  43. #define LM95234_REG_UTEMPL(x) ((x) + 0x29)
  44. #define LM95234_REG_REM_MODEL 0x30
  45. #define LM95234_REG_REM_MODEL_STS 0x38
  46. #define LM95234_REG_OFFSET(x) ((x) + 0x31) /* Remote only */
  47. #define LM95234_REG_TCRIT1(x) ((x) + 0x40)
  48. #define LM95234_REG_TCRIT2(x) ((x) + 0x49) /* Remote channel 1,2 */
  49. #define LM95234_REG_TCRIT_HYST 0x5a
  50. #define NATSEMI_MAN_ID 0x01
  51. #define LM95234_CHIP_ID 0x79
  52. /* Client data (each client gets its own) */
  53. struct lm95234_data {
  54. struct device *hwmon_dev;
  55. struct mutex update_lock;
  56. unsigned long last_updated, interval; /* in jiffies */
  57. bool valid; /* false until following fields are valid */
  58. /* registers values */
  59. int temp[5]; /* temperature (signed) */
  60. u32 status; /* fault/alarm status */
  61. u8 tcrit1[5]; /* critical temperature limit */
  62. u8 tcrit2[2]; /* high temperature limit */
  63. s8 toffset[4]; /* remote temperature offset */
  64. u8 thyst; /* common hysteresis */
  65. u8 sensor_type; /* temperature sensor type */
  66. };
  67. static int lm95234_read_temp(struct i2c_client *client, int index, int *t)
  68. {
  69. int val;
  70. u16 temp = 0;
  71. if (index) {
  72. val = i2c_smbus_read_byte_data(client,
  73. LM95234_REG_UTEMPH(index - 1));
  74. if (val < 0)
  75. return val;
  76. temp = val << 8;
  77. val = i2c_smbus_read_byte_data(client,
  78. LM95234_REG_UTEMPL(index - 1));
  79. if (val < 0)
  80. return val;
  81. temp |= val;
  82. *t = temp;
  83. }
  84. /*
  85. * Read signed temperature if unsigned temperature is 0,
  86. * or if this is the local sensor.
  87. */
  88. if (!temp) {
  89. val = i2c_smbus_read_byte_data(client,
  90. LM95234_REG_TEMPH(index));
  91. if (val < 0)
  92. return val;
  93. temp = val << 8;
  94. val = i2c_smbus_read_byte_data(client,
  95. LM95234_REG_TEMPL(index));
  96. if (val < 0)
  97. return val;
  98. temp |= val;
  99. *t = (s16)temp;
  100. }
  101. return 0;
  102. }
  103. static u16 update_intervals[] = { 143, 364, 1000, 2500 };
  104. /* Fill value cache. Must be called with update lock held. */
  105. static int lm95234_fill_cache(struct i2c_client *client)
  106. {
  107. struct lm95234_data *data = i2c_get_clientdata(client);
  108. int i, ret;
  109. ret = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
  110. if (ret < 0)
  111. return ret;
  112. data->interval = msecs_to_jiffies(update_intervals[ret & 0x03]);
  113. for (i = 0; i < ARRAY_SIZE(data->tcrit1); i++) {
  114. ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT1(i));
  115. if (ret < 0)
  116. return ret;
  117. data->tcrit1[i] = ret;
  118. }
  119. for (i = 0; i < ARRAY_SIZE(data->tcrit2); i++) {
  120. ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT2(i));
  121. if (ret < 0)
  122. return ret;
  123. data->tcrit2[i] = ret;
  124. }
  125. for (i = 0; i < ARRAY_SIZE(data->toffset); i++) {
  126. ret = i2c_smbus_read_byte_data(client, LM95234_REG_OFFSET(i));
  127. if (ret < 0)
  128. return ret;
  129. data->toffset[i] = ret;
  130. }
  131. ret = i2c_smbus_read_byte_data(client, LM95234_REG_TCRIT_HYST);
  132. if (ret < 0)
  133. return ret;
  134. data->thyst = ret;
  135. ret = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
  136. if (ret < 0)
  137. return ret;
  138. data->sensor_type = ret;
  139. return 0;
  140. }
  141. static int lm95234_update_device(struct i2c_client *client,
  142. struct lm95234_data *data)
  143. {
  144. int ret;
  145. mutex_lock(&data->update_lock);
  146. if (time_after(jiffies, data->last_updated + data->interval) ||
  147. !data->valid) {
  148. int i;
  149. if (!data->valid) {
  150. ret = lm95234_fill_cache(client);
  151. if (ret < 0)
  152. goto abort;
  153. }
  154. data->valid = false;
  155. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  156. ret = lm95234_read_temp(client, i, &data->temp[i]);
  157. if (ret < 0)
  158. goto abort;
  159. }
  160. ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_FAULT);
  161. if (ret < 0)
  162. goto abort;
  163. data->status = ret;
  164. ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT1);
  165. if (ret < 0)
  166. goto abort;
  167. data->status |= ret << 8;
  168. ret = i2c_smbus_read_byte_data(client, LM95234_REG_STS_TCRIT2);
  169. if (ret < 0)
  170. goto abort;
  171. data->status |= ret << 16;
  172. data->last_updated = jiffies;
  173. data->valid = true;
  174. }
  175. ret = 0;
  176. abort:
  177. mutex_unlock(&data->update_lock);
  178. return ret;
  179. }
  180. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  181. char *buf)
  182. {
  183. struct i2c_client *client = to_i2c_client(dev);
  184. struct lm95234_data *data = i2c_get_clientdata(client);
  185. int index = to_sensor_dev_attr(attr)->index;
  186. int ret = lm95234_update_device(client, data);
  187. if (ret)
  188. return ret;
  189. return sprintf(buf, "%d\n",
  190. DIV_ROUND_CLOSEST(data->temp[index] * 125, 32));
  191. }
  192. static ssize_t show_alarm(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. struct i2c_client *client = to_i2c_client(dev);
  196. struct lm95234_data *data = i2c_get_clientdata(client);
  197. u32 mask = to_sensor_dev_attr(attr)->index;
  198. int ret = lm95234_update_device(client, data);
  199. if (ret)
  200. return ret;
  201. return sprintf(buf, "%u", !!(data->status & mask));
  202. }
  203. static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  204. char *buf)
  205. {
  206. struct i2c_client *client = to_i2c_client(dev);
  207. struct lm95234_data *data = i2c_get_clientdata(client);
  208. u8 mask = to_sensor_dev_attr(attr)->index;
  209. int ret = lm95234_update_device(client, data);
  210. if (ret)
  211. return ret;
  212. return sprintf(buf, data->sensor_type & mask ? "1\n" : "2\n");
  213. }
  214. static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  215. const char *buf, size_t count)
  216. {
  217. struct i2c_client *client = to_i2c_client(dev);
  218. struct lm95234_data *data = i2c_get_clientdata(client);
  219. unsigned long val;
  220. u8 mask = to_sensor_dev_attr(attr)->index;
  221. int ret = lm95234_update_device(client, data);
  222. if (ret)
  223. return ret;
  224. ret = kstrtoul(buf, 10, &val);
  225. if (ret < 0)
  226. return ret;
  227. if (val != 1 && val != 2)
  228. return -EINVAL;
  229. mutex_lock(&data->update_lock);
  230. if (val == 1)
  231. data->sensor_type |= mask;
  232. else
  233. data->sensor_type &= ~mask;
  234. data->valid = false;
  235. i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
  236. data->sensor_type);
  237. mutex_unlock(&data->update_lock);
  238. return count;
  239. }
  240. static ssize_t show_tcrit2(struct device *dev, struct device_attribute *attr,
  241. char *buf)
  242. {
  243. struct i2c_client *client = to_i2c_client(dev);
  244. struct lm95234_data *data = i2c_get_clientdata(client);
  245. int index = to_sensor_dev_attr(attr)->index;
  246. int ret = lm95234_update_device(client, data);
  247. if (ret)
  248. return ret;
  249. return sprintf(buf, "%u", data->tcrit2[index] * 1000);
  250. }
  251. static ssize_t set_tcrit2(struct device *dev, struct device_attribute *attr,
  252. const char *buf, size_t count)
  253. {
  254. struct i2c_client *client = to_i2c_client(dev);
  255. struct lm95234_data *data = i2c_get_clientdata(client);
  256. int index = to_sensor_dev_attr(attr)->index;
  257. long val;
  258. int ret = lm95234_update_device(client, data);
  259. if (ret)
  260. return ret;
  261. ret = kstrtol(buf, 10, &val);
  262. if (ret < 0)
  263. return ret;
  264. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, index ? 255 : 127);
  265. mutex_lock(&data->update_lock);
  266. data->tcrit2[index] = val;
  267. i2c_smbus_write_byte_data(client, LM95234_REG_TCRIT2(index), val);
  268. mutex_unlock(&data->update_lock);
  269. return count;
  270. }
  271. static ssize_t show_tcrit2_hyst(struct device *dev,
  272. struct device_attribute *attr, char *buf)
  273. {
  274. struct i2c_client *client = to_i2c_client(dev);
  275. struct lm95234_data *data = i2c_get_clientdata(client);
  276. int index = to_sensor_dev_attr(attr)->index;
  277. int ret = lm95234_update_device(client, data);
  278. if (ret)
  279. return ret;
  280. /* Result can be negative, so be careful with unsigned operands */
  281. return sprintf(buf, "%d",
  282. ((int)data->tcrit2[index] - (int)data->thyst) * 1000);
  283. }
  284. static ssize_t show_tcrit1(struct device *dev, struct device_attribute *attr,
  285. char *buf)
  286. {
  287. struct i2c_client *client = to_i2c_client(dev);
  288. struct lm95234_data *data = i2c_get_clientdata(client);
  289. int index = to_sensor_dev_attr(attr)->index;
  290. return sprintf(buf, "%u", data->tcrit1[index] * 1000);
  291. }
  292. static ssize_t set_tcrit1(struct device *dev, struct device_attribute *attr,
  293. const char *buf, size_t count)
  294. {
  295. struct i2c_client *client = to_i2c_client(dev);
  296. struct lm95234_data *data = i2c_get_clientdata(client);
  297. int index = to_sensor_dev_attr(attr)->index;
  298. long val;
  299. int ret = lm95234_update_device(client, data);
  300. if (ret)
  301. return ret;
  302. ret = kstrtol(buf, 10, &val);
  303. if (ret < 0)
  304. return ret;
  305. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
  306. mutex_lock(&data->update_lock);
  307. data->tcrit1[index] = val;
  308. i2c_smbus_write_byte_data(client, LM95234_REG_TCRIT1(index), val);
  309. mutex_unlock(&data->update_lock);
  310. return count;
  311. }
  312. static ssize_t show_tcrit1_hyst(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. struct i2c_client *client = to_i2c_client(dev);
  316. struct lm95234_data *data = i2c_get_clientdata(client);
  317. int index = to_sensor_dev_attr(attr)->index;
  318. int ret = lm95234_update_device(client, data);
  319. if (ret)
  320. return ret;
  321. /* Result can be negative, so be careful with unsigned operands */
  322. return sprintf(buf, "%d",
  323. ((int)data->tcrit1[index] - (int)data->thyst) * 1000);
  324. }
  325. static ssize_t set_tcrit1_hyst(struct device *dev,
  326. struct device_attribute *attr,
  327. const char *buf, size_t count)
  328. {
  329. struct i2c_client *client = to_i2c_client(dev);
  330. struct lm95234_data *data = i2c_get_clientdata(client);
  331. int index = to_sensor_dev_attr(attr)->index;
  332. long val;
  333. int ret = lm95234_update_device(client, data);
  334. if (ret)
  335. return ret;
  336. ret = kstrtol(buf, 10, &val);
  337. if (ret < 0)
  338. return ret;
  339. val = DIV_ROUND_CLOSEST(val, 1000);
  340. val = clamp_val((int)data->tcrit1[index] - val, 0, 31);
  341. mutex_lock(&data->update_lock);
  342. data->thyst = val;
  343. i2c_smbus_write_byte_data(client, LM95234_REG_TCRIT_HYST, val);
  344. mutex_unlock(&data->update_lock);
  345. return count;
  346. }
  347. static ssize_t show_offset(struct device *dev, struct device_attribute *attr,
  348. char *buf)
  349. {
  350. struct i2c_client *client = to_i2c_client(dev);
  351. struct lm95234_data *data = i2c_get_clientdata(client);
  352. int index = to_sensor_dev_attr(attr)->index;
  353. int ret = lm95234_update_device(client, data);
  354. if (ret)
  355. return ret;
  356. return sprintf(buf, "%d", data->toffset[index] * 500);
  357. }
  358. static ssize_t set_offset(struct device *dev, struct device_attribute *attr,
  359. const char *buf, size_t count)
  360. {
  361. struct i2c_client *client = to_i2c_client(dev);
  362. struct lm95234_data *data = i2c_get_clientdata(client);
  363. int index = to_sensor_dev_attr(attr)->index;
  364. long val;
  365. int ret = lm95234_update_device(client, data);
  366. if (ret)
  367. return ret;
  368. ret = kstrtol(buf, 10, &val);
  369. if (ret < 0)
  370. return ret;
  371. /* Accuracy is 1/2 degrees C */
  372. val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127);
  373. mutex_lock(&data->update_lock);
  374. data->toffset[index] = val;
  375. i2c_smbus_write_byte_data(client, LM95234_REG_OFFSET(index), val);
  376. mutex_unlock(&data->update_lock);
  377. return count;
  378. }
  379. static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  380. char *buf)
  381. {
  382. struct i2c_client *client = to_i2c_client(dev);
  383. struct lm95234_data *data = i2c_get_clientdata(client);
  384. int ret = lm95234_update_device(client, data);
  385. if (ret)
  386. return ret;
  387. return sprintf(buf, "%lu\n",
  388. DIV_ROUND_CLOSEST(data->interval * 1000, HZ));
  389. }
  390. static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  391. const char *buf, size_t count)
  392. {
  393. struct i2c_client *client = to_i2c_client(dev);
  394. struct lm95234_data *data = i2c_get_clientdata(client);
  395. unsigned long val;
  396. u8 regval;
  397. int ret = lm95234_update_device(client, data);
  398. if (ret)
  399. return ret;
  400. ret = kstrtoul(buf, 10, &val);
  401. if (ret < 0)
  402. return ret;
  403. for (regval = 0; regval < 3; regval++) {
  404. if (val <= update_intervals[regval])
  405. break;
  406. }
  407. mutex_lock(&data->update_lock);
  408. data->interval = msecs_to_jiffies(update_intervals[regval]);
  409. i2c_smbus_write_byte_data(client, LM95234_REG_CONVRATE, regval);
  410. mutex_unlock(&data->update_lock);
  411. return count;
  412. }
  413. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  414. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  415. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  416. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
  417. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4);
  418. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
  419. BIT(0) | BIT(1));
  420. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL,
  421. BIT(2) | BIT(3));
  422. static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL,
  423. BIT(4) | BIT(5));
  424. static SENSOR_DEVICE_ATTR(temp5_fault, S_IRUGO, show_alarm, NULL,
  425. BIT(6) | BIT(7));
  426. static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
  427. BIT(1));
  428. static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
  429. BIT(2));
  430. static SENSOR_DEVICE_ATTR(temp4_type, S_IWUSR | S_IRUGO, show_type, set_type,
  431. BIT(3));
  432. static SENSOR_DEVICE_ATTR(temp5_type, S_IWUSR | S_IRUGO, show_type, set_type,
  433. BIT(4));
  434. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_tcrit1,
  435. set_tcrit1, 0);
  436. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_tcrit2,
  437. set_tcrit2, 0);
  438. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_tcrit2,
  439. set_tcrit2, 1);
  440. static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_tcrit1,
  441. set_tcrit1, 3);
  442. static SENSOR_DEVICE_ATTR(temp5_max, S_IWUSR | S_IRUGO, show_tcrit1,
  443. set_tcrit1, 4);
  444. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_tcrit1_hyst,
  445. set_tcrit1_hyst, 0);
  446. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 0);
  447. static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO, show_tcrit2_hyst, NULL, 1);
  448. static SENSOR_DEVICE_ATTR(temp4_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 3);
  449. static SENSOR_DEVICE_ATTR(temp5_max_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 4);
  450. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  451. BIT(0 + 8));
  452. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
  453. BIT(1 + 16));
  454. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
  455. BIT(2 + 16));
  456. static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
  457. BIT(3 + 8));
  458. static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
  459. BIT(4 + 8));
  460. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_tcrit1,
  461. set_tcrit1, 1);
  462. static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_tcrit1,
  463. set_tcrit1, 2);
  464. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 1);
  465. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_tcrit1_hyst, NULL, 2);
  466. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
  467. BIT(1 + 8));
  468. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
  469. BIT(2 + 8));
  470. static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_offset,
  471. set_offset, 0);
  472. static SENSOR_DEVICE_ATTR(temp3_offset, S_IWUSR | S_IRUGO, show_offset,
  473. set_offset, 1);
  474. static SENSOR_DEVICE_ATTR(temp4_offset, S_IWUSR | S_IRUGO, show_offset,
  475. set_offset, 2);
  476. static SENSOR_DEVICE_ATTR(temp5_offset, S_IWUSR | S_IRUGO, show_offset,
  477. set_offset, 3);
  478. static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
  479. set_interval);
  480. static struct attribute *lm95234_attributes[] = {
  481. &sensor_dev_attr_temp1_input.dev_attr.attr,
  482. &sensor_dev_attr_temp2_input.dev_attr.attr,
  483. &sensor_dev_attr_temp3_input.dev_attr.attr,
  484. &sensor_dev_attr_temp4_input.dev_attr.attr,
  485. &sensor_dev_attr_temp5_input.dev_attr.attr,
  486. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  487. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  488. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  489. &sensor_dev_attr_temp5_fault.dev_attr.attr,
  490. &sensor_dev_attr_temp2_type.dev_attr.attr,
  491. &sensor_dev_attr_temp3_type.dev_attr.attr,
  492. &sensor_dev_attr_temp4_type.dev_attr.attr,
  493. &sensor_dev_attr_temp5_type.dev_attr.attr,
  494. &sensor_dev_attr_temp1_max.dev_attr.attr,
  495. &sensor_dev_attr_temp2_max.dev_attr.attr,
  496. &sensor_dev_attr_temp3_max.dev_attr.attr,
  497. &sensor_dev_attr_temp4_max.dev_attr.attr,
  498. &sensor_dev_attr_temp5_max.dev_attr.attr,
  499. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  500. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  501. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  502. &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
  503. &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
  504. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  505. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  506. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  507. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  508. &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
  509. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  510. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  511. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  512. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  513. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  514. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  515. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  516. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  517. &sensor_dev_attr_temp4_offset.dev_attr.attr,
  518. &sensor_dev_attr_temp5_offset.dev_attr.attr,
  519. &dev_attr_update_interval.attr,
  520. NULL
  521. };
  522. static const struct attribute_group lm95234_group = {
  523. .attrs = lm95234_attributes,
  524. };
  525. static int lm95234_detect(struct i2c_client *client,
  526. struct i2c_board_info *info)
  527. {
  528. struct i2c_adapter *adapter = client->adapter;
  529. int mfg_id, chip_id, val;
  530. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  531. return -ENODEV;
  532. mfg_id = i2c_smbus_read_byte_data(client, LM95234_REG_MAN_ID);
  533. if (mfg_id != NATSEMI_MAN_ID)
  534. return -ENODEV;
  535. chip_id = i2c_smbus_read_byte_data(client, LM95234_REG_CHIP_ID);
  536. if (chip_id != LM95234_CHIP_ID)
  537. return -ENODEV;
  538. val = i2c_smbus_read_byte_data(client, LM95234_REG_STATUS);
  539. if (val & 0x30)
  540. return -ENODEV;
  541. val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
  542. if (val & 0xbc)
  543. return -ENODEV;
  544. val = i2c_smbus_read_byte_data(client, LM95234_REG_CONVRATE);
  545. if (val & 0xfc)
  546. return -ENODEV;
  547. val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
  548. if (val & 0xe1)
  549. return -ENODEV;
  550. val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
  551. if (val & 0xe1)
  552. return -ENODEV;
  553. strlcpy(info->type, "lm95234", I2C_NAME_SIZE);
  554. return 0;
  555. }
  556. static int lm95234_init_client(struct i2c_client *client)
  557. {
  558. int val, model;
  559. /* start conversion if necessary */
  560. val = i2c_smbus_read_byte_data(client, LM95234_REG_CONFIG);
  561. if (val < 0)
  562. return val;
  563. if (val & 0x40)
  564. i2c_smbus_write_byte_data(client, LM95234_REG_CONFIG,
  565. val & ~0x40);
  566. /* If diode type status reports an error, try to fix it */
  567. val = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL_STS);
  568. if (val < 0)
  569. return val;
  570. model = i2c_smbus_read_byte_data(client, LM95234_REG_REM_MODEL);
  571. if (model < 0)
  572. return model;
  573. if (model & val) {
  574. dev_notice(&client->dev,
  575. "Fixing remote diode type misconfiguration (0x%x)\n",
  576. val);
  577. i2c_smbus_write_byte_data(client, LM95234_REG_REM_MODEL,
  578. model & ~val);
  579. }
  580. return 0;
  581. }
  582. static int lm95234_probe(struct i2c_client *client,
  583. const struct i2c_device_id *id)
  584. {
  585. struct device *dev = &client->dev;
  586. struct lm95234_data *data;
  587. int err;
  588. data = devm_kzalloc(dev, sizeof(struct lm95234_data), GFP_KERNEL);
  589. if (!data)
  590. return -ENOMEM;
  591. i2c_set_clientdata(client, data);
  592. mutex_init(&data->update_lock);
  593. /* Initialize the LM95234 chip */
  594. err = lm95234_init_client(client);
  595. if (err < 0)
  596. return err;
  597. /* Register sysfs hooks */
  598. err = sysfs_create_group(&dev->kobj, &lm95234_group);
  599. if (err)
  600. return err;
  601. data->hwmon_dev = hwmon_device_register(dev);
  602. if (IS_ERR(data->hwmon_dev)) {
  603. err = PTR_ERR(data->hwmon_dev);
  604. goto exit_remove_files;
  605. }
  606. return 0;
  607. exit_remove_files:
  608. sysfs_remove_group(&dev->kobj, &lm95234_group);
  609. return err;
  610. }
  611. static int lm95234_remove(struct i2c_client *client)
  612. {
  613. struct lm95234_data *data = i2c_get_clientdata(client);
  614. hwmon_device_unregister(data->hwmon_dev);
  615. sysfs_remove_group(&client->dev.kobj, &lm95234_group);
  616. return 0;
  617. }
  618. /* Driver data (common to all clients) */
  619. static const struct i2c_device_id lm95234_id[] = {
  620. { "lm95234", 0 },
  621. { }
  622. };
  623. MODULE_DEVICE_TABLE(i2c, lm95234_id);
  624. static struct i2c_driver lm95234_driver = {
  625. .class = I2C_CLASS_HWMON,
  626. .driver = {
  627. .name = DRVNAME,
  628. },
  629. .probe = lm95234_probe,
  630. .remove = lm95234_remove,
  631. .id_table = lm95234_id,
  632. .detect = lm95234_detect,
  633. .address_list = normal_i2c,
  634. };
  635. module_i2c_driver(lm95234_driver);
  636. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  637. MODULE_DESCRIPTION("LM95234 sensor driver");
  638. MODULE_LICENSE("GPL");