stts751.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * STTS751 sensor driver
  4. *
  5. * Copyright (C) 2016-2017 Istituto Italiano di Tecnologia - RBCS - EDL
  6. * Robotics, Brain and Cognitive Sciences department
  7. * Electronic Design Laboratory
  8. *
  9. * Written by Andrea Merello <[email protected]>
  10. *
  11. * Based on LM95241 driver and LM90 driver
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/err.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/i2c.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/property.h>
  24. #include <linux/slab.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/util_macros.h>
  27. #define DEVNAME "stts751"
  28. static const unsigned short normal_i2c[] = {
  29. 0x48, 0x49, 0x38, 0x39, /* STTS751-0 */
  30. 0x4A, 0x4B, 0x3A, 0x3B, /* STTS751-1 */
  31. I2C_CLIENT_END };
  32. #define STTS751_REG_TEMP_H 0x00
  33. #define STTS751_REG_STATUS 0x01
  34. #define STTS751_STATUS_TRIPT BIT(0)
  35. #define STTS751_STATUS_TRIPL BIT(5)
  36. #define STTS751_STATUS_TRIPH BIT(6)
  37. #define STTS751_REG_TEMP_L 0x02
  38. #define STTS751_REG_CONF 0x03
  39. #define STTS751_CONF_RES_MASK 0x0C
  40. #define STTS751_CONF_RES_SHIFT 2
  41. #define STTS751_CONF_EVENT_DIS BIT(7)
  42. #define STTS751_CONF_STOP BIT(6)
  43. #define STTS751_REG_RATE 0x04
  44. #define STTS751_REG_HLIM_H 0x05
  45. #define STTS751_REG_HLIM_L 0x06
  46. #define STTS751_REG_LLIM_H 0x07
  47. #define STTS751_REG_LLIM_L 0x08
  48. #define STTS751_REG_TLIM 0x20
  49. #define STTS751_REG_HYST 0x21
  50. #define STTS751_REG_SMBUS_TO 0x22
  51. #define STTS751_REG_PROD_ID 0xFD
  52. #define STTS751_REG_MAN_ID 0xFE
  53. #define STTS751_REG_REV_ID 0xFF
  54. #define STTS751_0_PROD_ID 0x00
  55. #define STTS751_1_PROD_ID 0x01
  56. #define ST_MAN_ID 0x53
  57. /*
  58. * Possible update intervals are (in mS):
  59. * 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 62.5, 31.25
  60. * However we are not going to complicate things too much and we stick to the
  61. * approx value in mS.
  62. */
  63. static const int stts751_intervals[] = {
  64. 16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 31
  65. };
  66. static const struct i2c_device_id stts751_id[] = {
  67. { "stts751", 0 },
  68. { }
  69. };
  70. static const struct of_device_id __maybe_unused stts751_of_match[] = {
  71. { .compatible = "stts751" },
  72. { },
  73. };
  74. MODULE_DEVICE_TABLE(of, stts751_of_match);
  75. struct stts751_priv {
  76. struct device *dev;
  77. struct i2c_client *client;
  78. struct mutex access_lock;
  79. u8 interval;
  80. int res;
  81. int event_max, event_min;
  82. int therm;
  83. int hyst;
  84. bool smbus_timeout;
  85. int temp;
  86. unsigned long last_update, last_alert_update;
  87. u8 config;
  88. bool min_alert, max_alert, therm_trip;
  89. bool data_valid, alert_valid;
  90. bool notify_max, notify_min;
  91. };
  92. /*
  93. * These functions converts temperature from HW format to integer format and
  94. * vice-vers. They are (mostly) taken from lm90 driver. Unit is in mC.
  95. */
  96. static int stts751_to_deg(s16 hw_val)
  97. {
  98. return hw_val * 125 / 32;
  99. }
  100. static s32 stts751_to_hw(int val)
  101. {
  102. return DIV_ROUND_CLOSEST(val, 125) * 32;
  103. }
  104. static int stts751_adjust_resolution(struct stts751_priv *priv)
  105. {
  106. u8 res;
  107. switch (priv->interval) {
  108. case 9:
  109. /* 10 bits */
  110. res = 0;
  111. break;
  112. case 8:
  113. /* 11 bits */
  114. res = 1;
  115. break;
  116. default:
  117. /* 12 bits */
  118. res = 3;
  119. break;
  120. }
  121. if (priv->res == res)
  122. return 0;
  123. priv->config &= ~STTS751_CONF_RES_MASK;
  124. priv->config |= res << STTS751_CONF_RES_SHIFT;
  125. dev_dbg(&priv->client->dev, "setting res %d. config %x",
  126. res, priv->config);
  127. priv->res = res;
  128. return i2c_smbus_write_byte_data(priv->client,
  129. STTS751_REG_CONF, priv->config);
  130. }
  131. static int stts751_update_temp(struct stts751_priv *priv)
  132. {
  133. s32 integer1, integer2, frac;
  134. /*
  135. * There is a trick here, like in the lm90 driver. We have to read two
  136. * registers to get the sensor temperature, but we have to beware a
  137. * conversion could occur between the readings. We could use the
  138. * one-shot conversion register, but we don't want to do this (disables
  139. * hardware monitoring). So the solution used here is to read the high
  140. * byte once, then the low byte, then the high byte again. If the new
  141. * high byte matches the old one, then we have a valid reading. Else we
  142. * have to read the low byte again, and now we believe we have a correct
  143. * reading.
  144. */
  145. integer1 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
  146. if (integer1 < 0) {
  147. dev_dbg(&priv->client->dev,
  148. "I2C read failed (temp H). ret: %x\n", integer1);
  149. return integer1;
  150. }
  151. frac = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_L);
  152. if (frac < 0) {
  153. dev_dbg(&priv->client->dev,
  154. "I2C read failed (temp L). ret: %x\n", frac);
  155. return frac;
  156. }
  157. integer2 = i2c_smbus_read_byte_data(priv->client, STTS751_REG_TEMP_H);
  158. if (integer2 < 0) {
  159. dev_dbg(&priv->client->dev,
  160. "I2C 2nd read failed (temp H). ret: %x\n", integer2);
  161. return integer2;
  162. }
  163. if (integer1 != integer2) {
  164. frac = i2c_smbus_read_byte_data(priv->client,
  165. STTS751_REG_TEMP_L);
  166. if (frac < 0) {
  167. dev_dbg(&priv->client->dev,
  168. "I2C 2nd read failed (temp L). ret: %x\n",
  169. frac);
  170. return frac;
  171. }
  172. }
  173. priv->temp = stts751_to_deg((integer1 << 8) | frac);
  174. return 0;
  175. }
  176. static int stts751_set_temp_reg16(struct stts751_priv *priv, int temp,
  177. u8 hreg, u8 lreg)
  178. {
  179. s32 hwval;
  180. int ret;
  181. hwval = stts751_to_hw(temp);
  182. ret = i2c_smbus_write_byte_data(priv->client, hreg, hwval >> 8);
  183. if (ret)
  184. return ret;
  185. return i2c_smbus_write_byte_data(priv->client, lreg, hwval & 0xff);
  186. }
  187. static int stts751_set_temp_reg8(struct stts751_priv *priv, int temp, u8 reg)
  188. {
  189. s32 hwval;
  190. hwval = stts751_to_hw(temp);
  191. return i2c_smbus_write_byte_data(priv->client, reg, hwval >> 8);
  192. }
  193. static int stts751_read_reg16(struct stts751_priv *priv, int *temp,
  194. u8 hreg, u8 lreg)
  195. {
  196. int integer, frac;
  197. integer = i2c_smbus_read_byte_data(priv->client, hreg);
  198. if (integer < 0)
  199. return integer;
  200. frac = i2c_smbus_read_byte_data(priv->client, lreg);
  201. if (frac < 0)
  202. return frac;
  203. *temp = stts751_to_deg((integer << 8) | frac);
  204. return 0;
  205. }
  206. static int stts751_read_reg8(struct stts751_priv *priv, int *temp, u8 reg)
  207. {
  208. int integer;
  209. integer = i2c_smbus_read_byte_data(priv->client, reg);
  210. if (integer < 0)
  211. return integer;
  212. *temp = stts751_to_deg(integer << 8);
  213. return 0;
  214. }
  215. /*
  216. * Update alert flags without waiting for cache to expire. We detects alerts
  217. * immediately for the sake of the alert handler; we still need to deal with
  218. * caching to workaround the fact that alarm flags int the status register,
  219. * despite what the datasheet claims, gets always cleared on read.
  220. */
  221. static int stts751_update_alert(struct stts751_priv *priv)
  222. {
  223. int ret;
  224. bool conv_done;
  225. int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
  226. /*
  227. * Add another 10% because if we run faster than the HW conversion
  228. * rate we will end up in reporting incorrectly alarms.
  229. */
  230. cache_time += cache_time / 10;
  231. ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_STATUS);
  232. if (ret < 0)
  233. return ret;
  234. dev_dbg(&priv->client->dev, "status reg %x\n", ret);
  235. conv_done = ret & (STTS751_STATUS_TRIPH | STTS751_STATUS_TRIPL);
  236. /*
  237. * Reset the cache if the cache time expired, or if we are sure
  238. * we have valid data from a device conversion, or if we know
  239. * our cache has been never written.
  240. *
  241. * Note that when the cache has been never written the point is
  242. * to correctly initialize the timestamp, rather than clearing
  243. * the cache values.
  244. *
  245. * Note that updating the cache timestamp when we get an alarm flag
  246. * is required, otherwise we could incorrectly report alarms to be zero.
  247. */
  248. if (time_after(jiffies, priv->last_alert_update + cache_time) ||
  249. conv_done || !priv->alert_valid) {
  250. priv->max_alert = false;
  251. priv->min_alert = false;
  252. priv->alert_valid = true;
  253. priv->last_alert_update = jiffies;
  254. dev_dbg(&priv->client->dev, "invalidating alert cache\n");
  255. }
  256. priv->max_alert |= !!(ret & STTS751_STATUS_TRIPH);
  257. priv->min_alert |= !!(ret & STTS751_STATUS_TRIPL);
  258. priv->therm_trip = !!(ret & STTS751_STATUS_TRIPT);
  259. dev_dbg(&priv->client->dev, "max_alert: %d, min_alert: %d, therm_trip: %d\n",
  260. priv->max_alert, priv->min_alert, priv->therm_trip);
  261. return 0;
  262. }
  263. static void stts751_alert(struct i2c_client *client,
  264. enum i2c_alert_protocol type, unsigned int data)
  265. {
  266. int ret;
  267. struct stts751_priv *priv = i2c_get_clientdata(client);
  268. if (type != I2C_PROTOCOL_SMBUS_ALERT)
  269. return;
  270. dev_dbg(&client->dev, "alert!");
  271. mutex_lock(&priv->access_lock);
  272. ret = stts751_update_alert(priv);
  273. if (ret < 0) {
  274. /* default to worst case */
  275. priv->max_alert = true;
  276. priv->min_alert = true;
  277. dev_warn(priv->dev,
  278. "Alert received, but can't communicate to the device. Triggering all alarms!");
  279. }
  280. if (priv->max_alert) {
  281. if (priv->notify_max)
  282. dev_notice(priv->dev, "got alert for HIGH temperature");
  283. priv->notify_max = false;
  284. /* unblock alert poll */
  285. sysfs_notify(&priv->dev->kobj, NULL, "temp1_max_alarm");
  286. }
  287. if (priv->min_alert) {
  288. if (priv->notify_min)
  289. dev_notice(priv->dev, "got alert for LOW temperature");
  290. priv->notify_min = false;
  291. /* unblock alert poll */
  292. sysfs_notify(&priv->dev->kobj, NULL, "temp1_min_alarm");
  293. }
  294. if (priv->min_alert || priv->max_alert)
  295. kobject_uevent(&priv->dev->kobj, KOBJ_CHANGE);
  296. mutex_unlock(&priv->access_lock);
  297. }
  298. static int stts751_update(struct stts751_priv *priv)
  299. {
  300. int ret;
  301. int cache_time = msecs_to_jiffies(stts751_intervals[priv->interval]);
  302. if (time_after(jiffies, priv->last_update + cache_time) ||
  303. !priv->data_valid) {
  304. ret = stts751_update_temp(priv);
  305. if (ret)
  306. return ret;
  307. ret = stts751_update_alert(priv);
  308. if (ret)
  309. return ret;
  310. priv->data_valid = true;
  311. priv->last_update = jiffies;
  312. }
  313. return 0;
  314. }
  315. static ssize_t max_alarm_show(struct device *dev,
  316. struct device_attribute *attr, char *buf)
  317. {
  318. int ret;
  319. struct stts751_priv *priv = dev_get_drvdata(dev);
  320. mutex_lock(&priv->access_lock);
  321. ret = stts751_update(priv);
  322. if (!ret)
  323. priv->notify_max = true;
  324. mutex_unlock(&priv->access_lock);
  325. if (ret < 0)
  326. return ret;
  327. return sysfs_emit(buf, "%d\n", priv->max_alert);
  328. }
  329. static ssize_t min_alarm_show(struct device *dev,
  330. struct device_attribute *attr, char *buf)
  331. {
  332. int ret;
  333. struct stts751_priv *priv = dev_get_drvdata(dev);
  334. mutex_lock(&priv->access_lock);
  335. ret = stts751_update(priv);
  336. if (!ret)
  337. priv->notify_min = true;
  338. mutex_unlock(&priv->access_lock);
  339. if (ret < 0)
  340. return ret;
  341. return sysfs_emit(buf, "%d\n", priv->min_alert);
  342. }
  343. static ssize_t input_show(struct device *dev, struct device_attribute *attr,
  344. char *buf)
  345. {
  346. int ret;
  347. struct stts751_priv *priv = dev_get_drvdata(dev);
  348. mutex_lock(&priv->access_lock);
  349. ret = stts751_update(priv);
  350. mutex_unlock(&priv->access_lock);
  351. if (ret < 0)
  352. return ret;
  353. return sysfs_emit(buf, "%d\n", priv->temp);
  354. }
  355. static ssize_t therm_show(struct device *dev, struct device_attribute *attr,
  356. char *buf)
  357. {
  358. struct stts751_priv *priv = dev_get_drvdata(dev);
  359. return sysfs_emit(buf, "%d\n", priv->therm);
  360. }
  361. static ssize_t therm_store(struct device *dev, struct device_attribute *attr,
  362. const char *buf, size_t count)
  363. {
  364. int ret;
  365. long temp;
  366. struct stts751_priv *priv = dev_get_drvdata(dev);
  367. if (kstrtol(buf, 10, &temp) < 0)
  368. return -EINVAL;
  369. /* HW works in range -64C to +127.937C */
  370. temp = clamp_val(temp, -64000, 127937);
  371. mutex_lock(&priv->access_lock);
  372. ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM);
  373. if (ret)
  374. goto exit;
  375. dev_dbg(&priv->client->dev, "setting therm %ld", temp);
  376. /*
  377. * hysteresis reg is relative to therm, so the HW does not need to be
  378. * adjusted, we need to update our local copy only.
  379. */
  380. priv->hyst = temp - (priv->therm - priv->hyst);
  381. priv->therm = temp;
  382. exit:
  383. mutex_unlock(&priv->access_lock);
  384. if (ret)
  385. return ret;
  386. return count;
  387. }
  388. static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
  389. char *buf)
  390. {
  391. struct stts751_priv *priv = dev_get_drvdata(dev);
  392. return sysfs_emit(buf, "%d\n", priv->hyst);
  393. }
  394. static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
  395. const char *buf, size_t count)
  396. {
  397. int ret;
  398. long temp;
  399. struct stts751_priv *priv = dev_get_drvdata(dev);
  400. if (kstrtol(buf, 10, &temp) < 0)
  401. return -EINVAL;
  402. mutex_lock(&priv->access_lock);
  403. /* HW works in range -64C to +127.937C */
  404. temp = clamp_val(temp, -64000, priv->therm);
  405. priv->hyst = temp;
  406. dev_dbg(&priv->client->dev, "setting hyst %ld", temp);
  407. temp = priv->therm - temp;
  408. ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_HYST);
  409. mutex_unlock(&priv->access_lock);
  410. if (ret)
  411. return ret;
  412. return count;
  413. }
  414. static ssize_t therm_trip_show(struct device *dev,
  415. struct device_attribute *attr, char *buf)
  416. {
  417. int ret;
  418. struct stts751_priv *priv = dev_get_drvdata(dev);
  419. mutex_lock(&priv->access_lock);
  420. ret = stts751_update(priv);
  421. mutex_unlock(&priv->access_lock);
  422. if (ret < 0)
  423. return ret;
  424. return sysfs_emit(buf, "%d\n", priv->therm_trip);
  425. }
  426. static ssize_t max_show(struct device *dev, struct device_attribute *attr,
  427. char *buf)
  428. {
  429. struct stts751_priv *priv = dev_get_drvdata(dev);
  430. return sysfs_emit(buf, "%d\n", priv->event_max);
  431. }
  432. static ssize_t max_store(struct device *dev, struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. int ret;
  436. long temp;
  437. struct stts751_priv *priv = dev_get_drvdata(dev);
  438. if (kstrtol(buf, 10, &temp) < 0)
  439. return -EINVAL;
  440. mutex_lock(&priv->access_lock);
  441. /* HW works in range -64C to +127.937C */
  442. temp = clamp_val(temp, priv->event_min, 127937);
  443. ret = stts751_set_temp_reg16(priv, temp,
  444. STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
  445. if (ret)
  446. goto exit;
  447. dev_dbg(&priv->client->dev, "setting event max %ld", temp);
  448. priv->event_max = temp;
  449. ret = count;
  450. exit:
  451. mutex_unlock(&priv->access_lock);
  452. return ret;
  453. }
  454. static ssize_t min_show(struct device *dev, struct device_attribute *attr,
  455. char *buf)
  456. {
  457. struct stts751_priv *priv = dev_get_drvdata(dev);
  458. return sysfs_emit(buf, "%d\n", priv->event_min);
  459. }
  460. static ssize_t min_store(struct device *dev, struct device_attribute *attr,
  461. const char *buf, size_t count)
  462. {
  463. int ret;
  464. long temp;
  465. struct stts751_priv *priv = dev_get_drvdata(dev);
  466. if (kstrtol(buf, 10, &temp) < 0)
  467. return -EINVAL;
  468. mutex_lock(&priv->access_lock);
  469. /* HW works in range -64C to +127.937C */
  470. temp = clamp_val(temp, -64000, priv->event_max);
  471. ret = stts751_set_temp_reg16(priv, temp,
  472. STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
  473. if (ret)
  474. goto exit;
  475. dev_dbg(&priv->client->dev, "setting event min %ld", temp);
  476. priv->event_min = temp;
  477. ret = count;
  478. exit:
  479. mutex_unlock(&priv->access_lock);
  480. return ret;
  481. }
  482. static ssize_t interval_show(struct device *dev,
  483. struct device_attribute *attr, char *buf)
  484. {
  485. struct stts751_priv *priv = dev_get_drvdata(dev);
  486. return sysfs_emit(buf, "%d\n",
  487. stts751_intervals[priv->interval]);
  488. }
  489. static ssize_t interval_store(struct device *dev,
  490. struct device_attribute *attr, const char *buf,
  491. size_t count)
  492. {
  493. unsigned long val;
  494. int idx;
  495. int ret = count;
  496. struct stts751_priv *priv = dev_get_drvdata(dev);
  497. if (kstrtoul(buf, 10, &val) < 0)
  498. return -EINVAL;
  499. idx = find_closest_descending(val, stts751_intervals,
  500. ARRAY_SIZE(stts751_intervals));
  501. dev_dbg(&priv->client->dev, "setting interval. req:%lu, idx: %d, val: %d",
  502. val, idx, stts751_intervals[idx]);
  503. mutex_lock(&priv->access_lock);
  504. if (priv->interval == idx)
  505. goto exit;
  506. /*
  507. * In early development stages I've become suspicious about the chip
  508. * starting to misbehave if I ever set, even briefly, an invalid
  509. * configuration. While I'm not sure this is really needed, be
  510. * conservative and set rate/resolution in such an order that avoids
  511. * passing through an invalid configuration.
  512. */
  513. /* speed up: lower the resolution, then modify convrate */
  514. if (priv->interval < idx) {
  515. dev_dbg(&priv->client->dev, "lower resolution, then modify convrate");
  516. priv->interval = idx;
  517. ret = stts751_adjust_resolution(priv);
  518. if (ret)
  519. goto exit;
  520. }
  521. ret = i2c_smbus_write_byte_data(priv->client, STTS751_REG_RATE, idx);
  522. if (ret)
  523. goto exit;
  524. /* slow down: modify convrate, then raise resolution */
  525. if (priv->interval != idx) {
  526. dev_dbg(&priv->client->dev, "modify convrate, then raise resolution");
  527. priv->interval = idx;
  528. ret = stts751_adjust_resolution(priv);
  529. if (ret)
  530. goto exit;
  531. }
  532. ret = count;
  533. exit:
  534. mutex_unlock(&priv->access_lock);
  535. return ret;
  536. }
  537. static int stts751_detect(struct i2c_client *new_client,
  538. struct i2c_board_info *info)
  539. {
  540. struct i2c_adapter *adapter = new_client->adapter;
  541. const char *name;
  542. int tmp;
  543. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  544. return -ENODEV;
  545. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_MAN_ID);
  546. if (tmp != ST_MAN_ID)
  547. return -ENODEV;
  548. /* lower temperaure registers always have bits 0-3 set to zero */
  549. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_TEMP_L);
  550. if (tmp & 0xf)
  551. return -ENODEV;
  552. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_HLIM_L);
  553. if (tmp & 0xf)
  554. return -ENODEV;
  555. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_LLIM_L);
  556. if (tmp & 0xf)
  557. return -ENODEV;
  558. /* smbus timeout register always have bits 0-7 set to zero */
  559. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_SMBUS_TO);
  560. if (tmp & 0x7f)
  561. return -ENODEV;
  562. tmp = i2c_smbus_read_byte_data(new_client, STTS751_REG_PROD_ID);
  563. switch (tmp) {
  564. case STTS751_0_PROD_ID:
  565. name = "STTS751-0";
  566. break;
  567. case STTS751_1_PROD_ID:
  568. name = "STTS751-1";
  569. break;
  570. default:
  571. return -ENODEV;
  572. }
  573. dev_dbg(&new_client->dev, "Chip %s detected", name);
  574. strscpy(info->type, stts751_id[0].name, I2C_NAME_SIZE);
  575. return 0;
  576. }
  577. static int stts751_read_chip_config(struct stts751_priv *priv)
  578. {
  579. int ret;
  580. int tmp;
  581. ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_CONF);
  582. if (ret < 0)
  583. return ret;
  584. priv->config = ret;
  585. priv->res = (ret & STTS751_CONF_RES_MASK) >> STTS751_CONF_RES_SHIFT;
  586. ret = i2c_smbus_read_byte_data(priv->client, STTS751_REG_RATE);
  587. if (ret < 0)
  588. return ret;
  589. if (ret >= ARRAY_SIZE(stts751_intervals)) {
  590. dev_err(priv->dev, "Unrecognized conversion rate 0x%x\n", ret);
  591. return -ENODEV;
  592. }
  593. priv->interval = ret;
  594. ret = stts751_read_reg16(priv, &priv->event_max,
  595. STTS751_REG_HLIM_H, STTS751_REG_HLIM_L);
  596. if (ret)
  597. return ret;
  598. ret = stts751_read_reg16(priv, &priv->event_min,
  599. STTS751_REG_LLIM_H, STTS751_REG_LLIM_L);
  600. if (ret)
  601. return ret;
  602. ret = stts751_read_reg8(priv, &priv->therm, STTS751_REG_TLIM);
  603. if (ret)
  604. return ret;
  605. ret = stts751_read_reg8(priv, &tmp, STTS751_REG_HYST);
  606. if (ret)
  607. return ret;
  608. priv->hyst = priv->therm - tmp;
  609. return 0;
  610. }
  611. static SENSOR_DEVICE_ATTR_RO(temp1_input, input, 0);
  612. static SENSOR_DEVICE_ATTR_RW(temp1_min, min, 0);
  613. static SENSOR_DEVICE_ATTR_RW(temp1_max, max, 0);
  614. static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, min_alarm, 0);
  615. static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, max_alarm, 0);
  616. static SENSOR_DEVICE_ATTR_RW(temp1_crit, therm, 0);
  617. static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0);
  618. static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, therm_trip, 0);
  619. static SENSOR_DEVICE_ATTR_RW(update_interval, interval, 0);
  620. static struct attribute *stts751_attrs[] = {
  621. &sensor_dev_attr_temp1_input.dev_attr.attr,
  622. &sensor_dev_attr_temp1_min.dev_attr.attr,
  623. &sensor_dev_attr_temp1_max.dev_attr.attr,
  624. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  625. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  626. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  627. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  628. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  629. &sensor_dev_attr_update_interval.dev_attr.attr,
  630. NULL
  631. };
  632. ATTRIBUTE_GROUPS(stts751);
  633. static int stts751_probe(struct i2c_client *client)
  634. {
  635. struct stts751_priv *priv;
  636. int ret;
  637. bool smbus_nto;
  638. int rev_id;
  639. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  640. if (!priv)
  641. return -ENOMEM;
  642. priv->client = client;
  643. priv->notify_max = true;
  644. priv->notify_min = true;
  645. i2c_set_clientdata(client, priv);
  646. mutex_init(&priv->access_lock);
  647. if (device_property_present(&client->dev,
  648. "smbus-timeout-disable")) {
  649. smbus_nto = device_property_read_bool(&client->dev,
  650. "smbus-timeout-disable");
  651. ret = i2c_smbus_write_byte_data(client, STTS751_REG_SMBUS_TO,
  652. smbus_nto ? 0 : 0x80);
  653. if (ret)
  654. return ret;
  655. }
  656. rev_id = i2c_smbus_read_byte_data(client, STTS751_REG_REV_ID);
  657. if (rev_id < 0)
  658. return -ENODEV;
  659. if (rev_id != 0x1) {
  660. dev_dbg(&client->dev, "Chip revision 0x%x is untested\n",
  661. rev_id);
  662. }
  663. ret = stts751_read_chip_config(priv);
  664. if (ret)
  665. return ret;
  666. priv->config &= ~(STTS751_CONF_STOP | STTS751_CONF_EVENT_DIS);
  667. ret = i2c_smbus_write_byte_data(client, STTS751_REG_CONF, priv->config);
  668. if (ret)
  669. return ret;
  670. priv->dev = devm_hwmon_device_register_with_groups(&client->dev,
  671. client->name, priv,
  672. stts751_groups);
  673. return PTR_ERR_OR_ZERO(priv->dev);
  674. }
  675. MODULE_DEVICE_TABLE(i2c, stts751_id);
  676. static struct i2c_driver stts751_driver = {
  677. .class = I2C_CLASS_HWMON,
  678. .driver = {
  679. .name = DEVNAME,
  680. .of_match_table = of_match_ptr(stts751_of_match),
  681. },
  682. .probe_new = stts751_probe,
  683. .id_table = stts751_id,
  684. .detect = stts751_detect,
  685. .alert = stts751_alert,
  686. .address_list = normal_i2c,
  687. };
  688. module_i2c_driver(stts751_driver);
  689. MODULE_AUTHOR("Andrea Merello <[email protected]>");
  690. MODULE_DESCRIPTION("STTS751 sensor driver");
  691. MODULE_LICENSE("GPL");