lm95234.c 21 KB

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