stts751.c 21 KB

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